o
    Df                     @   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Zne	d
g7 Z	G dd
 d
ZG dd d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   new_sock Q/home/ubuntu/webapp/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   4   s   zDupFd.__init__.<locals>.sendc                      s   t   d S r
   )osr   r   r(   r   r   r   7   s   zDupFd.__init__.<locals>.close)r*   r   r   r   r   )r   fdr   r   r   r(   r   r   1   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&   /   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 | _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   C   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   )r3   r5   _startr.   r/   )r   r   r   r   r   r   r   M   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   V   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;   r3   r5   r   r?   r=   r   r   r6   joinis_aliver   sub_warningr4   r/   itemsclear)r   timeoutr;   rD   rC   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
   )r/   rH   rI   r0   appendr3   r1   r2   r4   r   r5   r6   )r   rC   r   r   r   r   r   r8   t   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>   rL   r4   r   debugr   r?   r=   rB   r5   r1   Thread_servedaemonstartr6   )r   rL   tr   r   r   r9      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signalrT   	SIG_BLOCKrangeNSIGr4   acceptrecvr/   popr   
is_exitingsys
excepthookexc_info)r   r   msgrC   destination_pidr   r   r   r   r   rP      s*   

z_ResourceSharer._server
   )r"   r#   r$   r%   r   r   staticmethodr   r   r8   r9   rP   r   r   r   r   r-   A   s    
	

r-   )r*   rV   r   r^   r1    r   r   r   __all__platformr   r&   r-   r   r   r   r   r   r   <module>   s"   



`
