
    Mh                         d 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	m
Z
mZ ddlZ edddg          Z G d	 d
e          Z G d d          Z e            ZdS )zcGarbage collection thread for representing zmq refcount of Python objects
used in zero-copy sends.
    N)
namedtuple)getpid)EventLockThreadgcrefobjeventc                   (     e Zd ZdZ fdZd Z xZS )GarbageCollectorThreadz4Thread in which garbage collection actually happens.c                     t                                                       || _        d| _        t	                      | _        t                      | _        d S )NT)super__init__gcdaemonr   pidr   ready)selfr   	__class__s     Q/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/zmq/utils/garbage.pyr   zGarbageCollectorThread.__init__   sA    88WW


    c                    t           t                      | j        k    r| j                                         d S 	 | j        j                            t          j                  }d|_	        |
                    | j        j                   | j                                         n# | j                                         w xY w	 t           t                      | j        k    rd S |                                }|dk    rnvt          |          dk    rdnd}t          j        ||          d         }| j        j                            |d           }|r |j        r|j                                         ~|                                 d S )Nr   T   DIE   LQ)r   r   r   setr   contextsocketzmqPULLlingerbindurlrecvlenstructunpackrefspopr
   close)r   smsgfmtkeytups         r   runzGarbageCollectorThread.run   sO   >VXX11JNNF	&&sx00AAHFF47;JNNDJNN	~TX!5!5&&((Cf}}SQ##CC-S))!,C',""3--C  sy  		 	
					s   AB$ $B?)__name__
__module____qualname____doc__r   r1   __classcell__r   s   @r   r   r      sM        >>          r   r   c                        e Zd ZdZdZdZdZdZd fd	Ze	d             Z
e
j        d             Z
d Zd Zd	 Zd
 Ze	d             Zd Zd ZddZd Z xZS )GarbageCollectoraF  PyZMQ Garbage Collector

    Used for representing the reference held by libzmq during zero-copy sends.
    This object holds a dictionary, keyed by Python id,
    of the Python objects whose memory are currently in use by zeromq.

    When zeromq is done with the memory, it sends a message on an inproc PUSH socket
    containing the packed size_t (32 or 64-bit unsigned int),
    which is the key in the dict.
    When the PULL socket in the gc thread receives that message,
    the reference is popped from the dict,
    and any tracker events that should be signaled fire.
    Nzinproc://pyzmq.gc.01c                    t                                                       i | _        d | _        d | _        || _        t                      | _        d| _        d | _	        d | _
        t          j        | j                   d S )NF)r   r   r)   r   thread_contextr   _lock
_stay_down_push_push_mutexatexitregister_atexit)r   r   r   s     r   r   zGarbageCollector.__init__N   sn    	VV

%%%%%r   c                     | j         Wt          j                            d          r ddlm} |                                | _         nt	          j                    | _         | j         S )Ngeventr   )green)r<   r   r3   
startswithr    rF   Context)r   rF   s     r   r   zGarbageCollector.contextZ   s[    =  ++H55 .%%%%%% % #}r   c                     |                                  r5| j        rt          j        dt                     |                                  || _        d S )Nz(Replacing gc context while gc is running)is_aliver)   warningswarnRuntimeWarningstopr<   )r   ctxs     r   r   zGarbageCollector.contextf   sO    ==?? 	y >   IIKKKr   c                 <    d| _         |                                  dS )zxatexit callback

        sets _stay_down flag so that gc doesn't try to start up again in other atexit handlers
        TN)r>   rN   r   s    r   rC   zGarbageCollector._atexitp   s    
 		r   c                 Z    |                                  sdS |                                  dS )z"stop the garbage-collection threadN)rJ   _stoprQ   s    r   rN   zGarbageCollector.stopx   s(    }} 	F

r   c                 p    d| _         d| _        d| _        | j                                         d| _        dS )zSClear state

        called after stop or when setting up a new subprocess
        N)r?   r@   r;   r)   clearr   rQ   s    r   _clearzGarbageCollector._clear~   s7    
 
	r   c                    | j                             t          j                  }|                    | j                   |                    d           |                                 | j        r| j                                         | j	        
                                 | j                                          |                                  d S )Nr   )r   r   r    PUSHconnectr$   sendr+   r?   r;   jointermrV   )r   pushs     r   rS   zGarbageCollector._stop   s    |""38,,TX		&

: 	Jr   c                    t          | dd          rt          d          |                                 r| j        H| j                            t          j                  | _        | j                            | j	                   | j        S )z?The PUSH socket for use in the zmq message destructor callback.r>   Fz'zmq gc socket requested during shutdown)
getattrRuntimeErrorrJ   r?   r   r   r    rX   rY   r$   rQ   s    r   _push_socketzGarbageCollector._push_socket   sx     4u-- 	JHIII}} 	)$*"4,,SX66DJJtx(((zr   c                 4   | j         +| j        t                      k    r|                                  t                      | _        i | _        t          |           | _         | j                                          | j         j                                         dS )zStart a new garbage collection thread.

        Creates a new zmq Context used for garbage collection.
        Under most circumstances, this will only be called once per process.
        N)	r;   r   r   rV   r)   r   startr   waitrQ   s    r   rc   zGarbageCollector.start   s}     ;"tx688';'; KKMMM88	,T22     r   c                     t           7t                      | j        k    s | j        | j                                        sdS dS )zsIs the garbage collection thread currently running?

        Includes checks for process shutdown or fork.
        NFT)r   r   r;   rJ   rQ   s    r   rJ   zGarbageCollector.is_alive   sD     Nxx48##{";'')) # 5tr   c                     |                                  sP| j        rdS | j        5  |                                  s|                                  ddd           n# 1 swxY w Y   t	          ||          }t          |          }|| j        |<   |S )z4store an object and (optionally) event for zero-copyr   N)rJ   r>   r=   rc   r   idr)   )r   r	   r
   r0   theids        r   storezGarbageCollector.store   s    }} 	! q  ! !}} !JJLLL! ! ! ! ! ! ! ! ! ! ! ! ! ! ! C3	%s   )AA!Ac                     |                                  sd S 	 |                                  d S # t          $ r}|d }~ww xY wN)rJ   rN   	Exception)r   es     r   __del__zGarbageCollector.__del__   sQ    }} 	F	IIKKKKK 	 	 	I	s   . 
?:?rk   )r2   r3   r4   r5   r)   r<   r=   r$   r   propertyr   setterrC   rN   rV   rS   ra   rc   rJ   ri   rn   r6   r7   s   @r   r9   r9   :   s-         DHE
 C
& 
& 
& 
& 
& 
& 	 	 X	 ^  ^    	 	 		 	 	   X! ! !             r   r9   )r5   rA   r'   rK   collectionsr   osr   	threadingr   r   r   r    r   r   r9   r    r   r   <module>ru      s        " " " " " "       ) ) ) ) ) ) ) ) ) ) 




7UG,--# # # # #V # # #LX X X X X X X Xv r   