o
    JAf                     @   s   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r<e
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 )    N   )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   Znew_sock 6/usr/lib/python3.10/multiprocessing/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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                   @   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   Znew_fdr   r   r   1   s   zDupFd.__init__.<locals>.sendc                      s   t   d S r
   )osr   r   r&   r   r   r   3   s   zDupFd.__init__.<locals>.close)r'   r   r   r   r   )r   fdr   r   r   r&   r   r   /   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   7   s   $zDupFd.detachNr   r   r   r   r   r$   -   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 resources using background thread.c                 C   s:   d| _ i | _t | _d | _d | _d | _t	| t
j d S )Nr   )_key_cache	threadingLock_lock	_listener_address_threadr   register_after_forkr*   
_afterfork)r   r   r   r   r   ?   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   )r/   r1   _startr+   r,   )r   r   r   r   r   r   r   H   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)
connectionr7   r   current_processr9   r   r'   getpid)identr7   addresskeycr   r   r   r   Q   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   r6   Nr8   z._ResourceSharer thread did not stop when asked)r:   r7   r/   r1   r   r;   r9   r   r   r2   joinis_aliver   sub_warningr0   r,   itemsclear)r   timeoutr7   r@   r?   r   r   r   r   r   r   Z   s*   




"z_ResourceSharer.stopc                 C   s\   | j  D ]	\}\}}|  q| j   | j  | jd ur#| j  d | _d | _d | _d S r
   )	r,   rD   rE   r/   _at_fork_reinitr0   r   r1   r2   )r   r?   r   r   r   r   r   r4   o   s   




z_ResourceSharer._afterforkc                 C   sX   ddl m} td |t jd| _| jj| _	t
j| jd}d|_|  || _d S )Nr   )Listenerz0starting listener and thread for sending handlesr8   )targetT)r:   rH   r   debugr   r;   r9   r0   r>   r1   r-   Thread_servedaemonstartr2   )r   rH   tr   r   r   r5   z   s   


z_ResourceSharer._startc                 C   s   t tdrttjt  	 zD| j 5}| }|d u r(	 W d    W d S |\}}| j	|\}}z
||| W |  n|  w W d    n1 sMw   Y  W n   t
 sbtjt   Y q)Npthread_sigmask)hasattrsignalrP   	SIG_BLOCKvalid_signalsr0   acceptrecvr,   popr   
is_exitingsys
excepthookexc_info)r   r   msgr?   destination_pidr   r   r   r   r   rL      s*   

z_ResourceSharer._server
   )r    r!   r"   r#   r   r   staticmethodr   r   r4   r5   rL   r   r   r   r   r*   =   s    		

r*   )r'   rR   r   rY   r-    r   contextr   r   __all__platformobjectr   r$   r*   r   r   r   r   r   r   <module>   s"   



\
