o
    svXjñ  ã                   @   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rBe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                   @   ó    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)ÚshareÚ
send_bytes)ÚconnÚpidr   ©Únew_sock© úU/var/www/html/myproject/venv/lib/python3.10/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 sw   Y  dS )z1Get the socket.  This should only be called once.N)r   Úget_connectionr   Ú
recv_bytesÚsocketÚ	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                   @   r
   )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   Úsend_handle)r   r   ©Ú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w   Y  dS )z-Get the fd.  This should only be called once.N)r   r   r   r   Ú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   Úregister_after_forkr.   Ú
_afterfork)r   r   r   r   r   D   s   
z_ResourceSharer.__init__c                 C   sf   | j �& | jdu r|  ¡  |  jd7  _||f| j| j< | j| jfW  d  ƒ S 1 s,w   Y  dS )z+Register resource, returning an identifier.Nr   )r4   r6   Ú_startr/   r0   )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)Ú
connectionr<   r   Úcurrent_processr>   r   r+   Úgetpid)Úidentr<   Ú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	 ¡ r2t d¡ | j ¡  d| _	d| _d| _| j ¡ D ]	\}\}}|ƒ  qE| j ¡  W d  ƒ dS W d  ƒ dS 1 sgw   Y  dS )z:Stop the background thread and clear registered resources.r   r;   Nr=   z._ResourceSharer thread did not stop when asked)r?   r<   r4   r6   r   r@   r>   r   r   r7   ÚjoinÚis_aliver   Úsub_warningr5   r0   ÚitemsÚclear)r   Útimeoutr<   rE   rD   r   r   r   r   r   r   `   s*   
ÿ



ð"ÿz_ResourceSharer.stopc                 C   sj   | j  ¡ D ]	\}\}}|ƒ  q| j  ¡  | j | j¡ t ¡ | _| jd ur*| j 	¡  d | _d | _
d | _d S r   )r0   rI   rJ   r1   Úappendr4   r2   r3   r5   r   r6   r7   )r   rD   r   r   r   r   r   r9   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 handlesr=   )ÚtargetT)r?   rM   r5   r   Údebugr   r@   r>   rC   r6   r2   ÚThreadÚ_serveÚdaemonÚstartr7   )r   rM   Útr   r   r   r:   ƒ   s   


z_ResourceSharer._startc                 C   sÌ   t tdƒrt tjtdtjƒ¡ 	 zD| j ¡ �5}| ¡ }|d u r*	 W d   ƒ W d S |\}}| j	 
|¡\}}z
|||ƒ W |ƒ  n|ƒ  w W d   ƒ n1 sOw   Y  W n   t ¡ sdtjt ¡ Ž  Y q)NÚpthread_sigmaskr   )ÚhasattrÚsignalrU   Ú	SIG_BLOCKÚrangeÚNSIGr5   ÚacceptÚrecvr0   Úpopr   Ú
is_exitingÚsysÚ
excepthookÚexc_info)r   r   ÚmsgrD   Údestination_pidr   r   r   r   r   rQ   Ž   s*   
ý÷€
€òz_ResourceSharer._server   )r#   r$   r%   r&   r   r   Ústaticmethodr   r   r9   r:   rQ   r   r   r   r   r.   B   s    
	

r.   )Ú
__future__r   r+   rW   r   r_   r2   Ú r   r   r   Ú__all__ÚplatformÚobjectr	   r'   r.   r   r   r   r   r   r   Ú<module>   s$   	


`
