o
    Zc                     @   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 N/var/www/chikooza/env/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$   	


`
