a
    xd                     @   s   d dl mZ d dlZd dlZd dlZd dlZd dlZddlmZ ddlm	Z	 ddlm
Z
 dgZejdkred	g7 ZG d
d	 d	eZnedg7 ZG dd deZG dd deZe ZejZdS )    )absolute_importN   )process)	reduction)utilstopwin32	DupSocketc                   @   s    e Zd ZdZdd Zdd ZdS )r	   zPicklable wrapper for a socket.c                    s(   |    fdd}t| j| _d S )Nc                    s     |}| | d S N)shareZ
send_bytes)connpidr   Znew_sock P/var/www/html/Ranjet/env/lib/python3.9/site-packages/billiard/resource_sharer.pysend"   s    
z DupSocket.__init__.<locals>.send)dup_resource_sharerregisterclose_id)selfsockr   r   r   r   __init__   s    zDupSocket.__init__c                 C   sB   t | j"}| }t|W  d   S 1 s40    Y  dS )z1Get the socket.  This should only be called once.N)r   get_connectionr   Z
recv_bytessocketZ	fromshare)r   r   r   r   r   r   detach'   s    zDupSocket.detachN__name__
__module____qualname____doc__r   r   r   r   r   r   r	      s   DupFdc                   @   s    e Zd ZdZdd Zdd ZdS )r"   z-Wrapper for fd which can be used at any time.c                    s4   t |  fdd} fdd}t||| _d S )Nc                    s   t |  | d S r
   )r   Zsend_handle)r   r   Znew_fdr   r   r   5   s    zDupFd.__init__.<locals>.sendc                      s   t   d S r
   )osr   r   r#   r   r   r   8   s    zDupFd.__init__.<locals>.close)r$   r   r   r   r   )r   fdr   r   r   r#   r   r   2   s    
zDupFd.__init__c                 C   s:   t | j}t|W  d   S 1 s,0    Y  dS )z-Get the fd.  This should only be called once.N)r   r   r   r   Zrecv_handle)r   r   r   r   r   r   <   s    zDupFd.detachNr   r   r   r   r   r"   0   s   
c                   @   sN   e Zd ZdZdd Zdd Zedd Zdd	d
Zdd Z	dd Z
dd ZdS )_ResourceSharerz-Manager for resouces using background thread.c                 C   s@   d| _ i | _g | _t | _d | _d | _d | _t	
| tj d S )Nr   )_key_cache
_old_locks	threadingLock_lock	_listener_address_threadr   Zregister_after_forkr&   
_afterfork)r   r   r   r   r   D   s    
z_ResourceSharer.__init__c                 C   sf   | j L | jdu r|   |  jd7  _||f| j| j< | j| jfW  d   S 1 sX0    Y  dS )z+Register resource, returning an identifier.Nr   )r,   r.   _startr'   r(   )r   r   r   r   r   r   r   N   s    
z_ResourceSharer.registerc                 C   s<   ddl m} | \}}||t jd}||t f |S )z<Return connection from which to receive identified resource.r   Clientauthkey)
connectionr3   r   current_processr5   r   r$   getpid)identr3   addresskeycr   r   r   r   W   s
    z_ResourceSharer.get_connectionNc                 C   s   ddl m} | j | jdur|| jt jd}|d |  | j	
| | j	 rdtd | j  d| _	d| _d| _| j D ]\}\}}|  q| j  W d   n1 s0    Y  dS )z:Stop the background thread and clear registered resources.r   r2   Nr4   z._ResourceSharer thread did not stop when asked)r6   r3   r,   r.   r   r7   r5   r   r   r/   joinis_aliver   Zsub_warningr-   r(   itemsclear)r   timeoutr3   r<   r;   r   r   r   r   r   r   `   s$    




z_ResourceSharer.stopc                 C   sj   | j  D ]\}\}}|  q
| j   | j| j t | _| jd urT| j	  d | _d | _
d | _d S r
   )r(   r?   r@   r)   appendr,   r*   r+   r-   r   r.   r/   )r   r;   r   r   r   r   r   r0   u   s    



z_ResourceSharer._afterforkc                 C   sf   ddl m} | jd u sJ td |t jd| _| jj| _	t
j| jd}d|_|  || _d S )Nr   )Listenerz0starting listener and thread for sending handlesr4   )targetT)r6   rC   r-   r   debugr   r7   r5   r:   r.   r*   Thread_servedaemonstartr/   )r   rC   tr   r   r   r1      s    

z_ResourceSharer._startc                 C   s   t tdr ttjtdtj z| j f}| }|d u rNW d    W q|\}}| j	
|\}}z||| W |  n|  0 W d    n1 s0    Y  W q    t stjt   Y q 0 q d S )Npthread_sigmaskr   )hasattrsignalrK   	SIG_BLOCKrangeNSIGr-   acceptrecvr(   popr   Z
is_exitingsys
excepthookexc_info)r   r   msgr;   Zdestination_pidr   r   r   r   r   rG      s    
2z_ResourceSharer._serve)N)r   r   r    r!   r   r   staticmethodr   r   r0   r1   rG   r   r   r   r   r&   B   s   
	

r&   )
__future__r   r$   rM   r   rT   r*    r   r   r   __all__platformobjectr	   r"   r&   r   r   r   r   r   r   <module>
   s"   


`