a
    If.                     @   s   g d Z ddlZddlZddl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
mZ ddlZddlmZ ddlmZ ejjZddlmZmZmZmZmZ G dd	 d	eZe ZG d
d deZG dd deZdS ))QueueSimpleQueueJoinableQueue    N)EmptyFull   )
connection)context)debuginfoFinalizeregister_after_fork
is_exitingc                   @   s   e Zd Zd-ddZdd Zdd Zdd	 Zd.ddZd/ddZd0ddZ	dd Z
dd Zdd Zdd Zdd Zdd Zdd  Zd!d" Zd#d$ Zed%d& Zed'd( Zed)d* Zed+d, ZdS )1r   r   c                C   s   |dkrddl m} || _tjdd\| _| _| | _t	
 | _tjdkrTd | _n
| | _||| _d| _|   tjdkrt| tj d S )Nr   r   )SEM_VALUE_MAXFZduplexwin32)Zsynchronizer   _maxsizer   Pipe_reader_writerLock_rlockosgetpid_opidsysplatform_wlockZBoundedSemaphore_sem_ignore_epipe_resetr   r   _after_forkselfmaxsizectx r&   'lib/python3.9/multiprocessing/queues.py__init__%   s    




zQueue.__init__c                 C   s.   t |  | j| j| j| j| j| j| j| j	fS N)
r	   assert_spawningr   r   r   r   r   r   r   r   r#   r&   r&   r'   __getstate__9   s    
zQueue.__getstate__c              	   C   s0   |\| _ | _| _| _| _| _| _| _|   d S r)   )	r   r   r   r   r   r   r   r   r    r#   stater&   r&   r'   __setstate__>   s    zQueue.__setstate__c                 C   s   t d | jdd d S )NzQueue._after_fork()T)
after_fork)r
   r    r+   r&   r&   r'   r!   C   s    zQueue._after_forkFc                 C   sj   |r| j   ntt | _ t | _d | _d | _	d| _
d| _d | _| jj| _| jj| _| jj| _d S NF)	_notempty_at_fork_reinit	threading	Conditionr   collectionsdeque_buffer_thread_jointhread_joincancelled_closed_closer   
send_bytes_send_bytesr   
recv_bytes_recv_bytespoll_poll)r#   r0   r&   r&   r'   r    G   s    


zQueue._resetTNc                 C   sz   | j rtd| d| j||s(t| j8 | jd u rB|   | j	| | j
  W d    n1 sl0    Y  d S NzQueue z
 is closed)r<   
ValueErrorr   acquirer   r2   r9   _start_threadr8   appendnotifyr#   objblocktimeoutr&   r&   r'   putV   s    
z	Queue.putc                 C   s   | j rtd| d|r\|d u r\| j |  }W d    n1 sF0    Y  | j  n||rlt | }| j||s~t	zL|r|t  }| 
|st	n| 
 st	|  }| j  W | j  n| j  0 t|S rD   )r<   rE   r   rA   r   releasetime	monotonicrF   r   rC   _ForkingPicklerloads)r#   rL   rM   resZdeadliner&   r&   r'   getb   s*    &
z	Queue.getc                 C   s   | j | jj  S r)   )r   r   _semlockZ
_get_valuer+   r&   r&   r'   qsize|   s    zQueue.qsizec                 C   s
   |    S r)   rC   r+   r&   r&   r'   empty   s    zQueue.emptyc                 C   s   | j j S r)   )r   rV   _is_zeror+   r&   r&   r'   full   s    z
Queue.fullc                 C   s
   |  dS r1   )rU   r+   r&   r&   r'   
get_nowait   s    zQueue.get_nowaitc                 C   s   |  |dS r1   )rN   r#   rK   r&   r&   r'   
put_nowait   s    zQueue.put_nowaitc                 C   s    d| _ | j}|rd | _|  d S )NT)r<   r=   )r#   closer&   r&   r'   r_      s
    zQueue.closec                 C   s.   t d | jsJ d| | jr*|   d S )NzQueue.join_thread()zQueue {0!r} not closed)r
   r<   formatr:   r+   r&   r&   r'   join_thread   s    zQueue.join_threadc                 C   s4   t d d| _z| j  W n ty.   Y n0 d S )NzQueue.cancel_join_thread()T)r
   r;   r:   ZcancelAttributeErrorr+   r&   r&   r'   cancel_join_thread   s    zQueue.cancel_join_threadc                 C   s   t d | j  tjtj| j| j| j| j	| j
j| jj| j| j| jf	dd| _d| j_t d | j  t d | jst| jtjt| jgdd| _t| tj| j| jgd	d| _d S )
NzQueue._start_thread()ZQueueFeederThread)targetargsnameTzdoing self._thread.start()z... done self._thread.start())Zexitpriority
   )r
   r8   clearr4   ZThreadr   _feedr2   r?   r   r   r_   r   r   _on_queue_feeder_errorr   r9   Zdaemonstartr;   r   _finalize_joinweakrefrefr:   _finalize_closer=   r+   r&   r&   r'   rG      s4    


zQueue._start_threadc                 C   s4   t d |  }|d ur(|  t d nt d d S )Nzjoining queue threadz... queue thread joinedz... queue thread already dead)r
   join)Ztwrthreadr&   r&   r'   rm      s    
zQueue._finalize_joinc                 C   sB   t d |" | t |  W d    n1 s40    Y  d S )Nztelling queue thread to quit)r
   rH   	_sentinelrI   )buffernotemptyr&   r&   r'   rp      s    
zQueue._finalize_closec	              
   C   sr  t d |j}	|j}
|j}| j}t}tjdkr<|j}|j}nd }z|	  z| sT|  W |
  n|
  0 zn| }||u rt d |  |  W W d S t	|}|d u r|| qh|  z|| W |  qh|  0 qhW n t
y   Y n0 W q@ tyj } zf|r$t|ddtjkr$W Y d }~d S t rDtd| W Y d }~d S |  ||| W Y d }~q@d }~0 0 q@d S )Nz$starting thread to feed data to piper   z%feeder thread got sentinel -- exitingerrnor   zerror in queue thread: %s)r
   rF   rO   waitpopleftrs   r   r   rR   dumps
IndexError	Exceptiongetattrrv   ZEPIPEr   r   )rt   ru   r>   Z	writelockZreader_closeZwriter_closeZignore_epipeonerrorZ	queue_semZnacquireZnreleaseZnwaitZbpopleftsentinelZwacquireZwreleaserK   er&   r&   r'   rj      sP    





zQueue._feedc                 C   s   ddl }|  dS )z
        Private API hook called when feeding data in the background thread
        raises an exception.  For overriding by concurrent.futures.
        r   N)	traceback	print_exc)r   rK   r   r&   r&   r'   rk     s    zQueue._on_queue_feeder_error)r   )F)TN)TN)__name__
__module____qualname__r(   r,   r/   r!   r    rN   rU   rW   rY   r[   r\   r^   r_   ra   rc   rG   staticmethodrm   rp   rj   rk   r&   r&   r&   r'   r   #   s0   



!
	

>r   c                   @   s@   e Zd ZdddZdd Zdd Zdd
dZdd Zdd Zd	S )r   r   c                C   s*   t j| ||d |d| _| | _d S )N)r%   r   )r   r(   Z	Semaphore_unfinished_tasksr5   _condr"   r&   r&   r'   r(   (  s    zJoinableQueue.__init__c                 C   s   t | | j| jf S r)   )r   r,   r   r   r+   r&   r&   r'   r,   -  s    zJoinableQueue.__getstate__c                 C   s,   t | |d d  |dd  \| _| _d S )N)r   r/   r   r   r-   r&   r&   r'   r/   0  s    zJoinableQueue.__setstate__TNc              	   C   s   | j rtd| d| j||s(t| jh | jB | jd u rJ|   | j	
| | j  | j  W d    n1 s~0    Y  W d    n1 s0    Y  d S rD   )r<   rE   r   rF   r   r2   r   r9   rG   r8   rH   r   rO   rI   rJ   r&   r&   r'   rN   4  s    

zJoinableQueue.putc                 C   sT   | j : | jdstd| jj r2| j   W d    n1 sF0    Y  d S )NFz!task_done() called too many times)r   r   rF   rE   rV   rZ   Z
notify_allr+   r&   r&   r'   	task_doneA  s
    zJoinableQueue.task_donec                 C   s@   | j & | jj s| j   W d    n1 s20    Y  d S r)   )r   r   rV   rZ   rw   r+   r&   r&   r'   rq   H  s    zJoinableQueue.join)r   )TN)	r   r   r   r(   r,   r/   rN   r   rq   r&   r&   r&   r'   r   &  s   

r   c                   @   sN   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd Zdd Z	e
ejZdS )r   c                C   sH   t jdd\| _| _| | _| jj| _tj	dkr:d | _
n
| | _
d S )NFr   r   )r   r   r   r   r   r   rB   rC   r   r   r   )r#   r%   r&   r&   r'   r(   S  s    


zSimpleQueue.__init__c                 C   s   | j   | j  d S r)   )r   r_   r   r+   r&   r&   r'   r_   \  s    
zSimpleQueue.closec                 C   s
   |    S r)   rX   r+   r&   r&   r'   rY   `  s    zSimpleQueue.emptyc                 C   s   t |  | j| j| j| jfS r)   )r	   r*   r   r   r   r   r+   r&   r&   r'   r,   c  s    
zSimpleQueue.__getstate__c                 C   s"   |\| _ | _| _| _| j j| _d S r)   )r   r   r   r   rB   rC   r-   r&   r&   r'   r/   g  s    zSimpleQueue.__setstate__c                 C   s:   | j  | j }W d    n1 s&0    Y  t|S r)   )r   r   r@   rR   rS   )r#   rT   r&   r&   r'   rU   k  s    (zSimpleQueue.getc                 C   sX   t |}| jd u r"| j| n2| j | j| W d    n1 sJ0    Y  d S r)   )rR   ry   r   r   r>   r]   r&   r&   r'   rN   q  s
    

zSimpleQueue.putN)r   r   r   r(   r_   rY   r,   r/   rU   rN   classmethodtypesGenericAlias__class_getitem__r&   r&   r&   r'   r   Q  s   	
r   )__all__r   r   r4   r6   rP   r   rn   rv   Zqueuer   r   Z_multiprocessing r   r	   Z	reductionZForkingPicklerrR   utilr
   r   r   r   r   objectr   rs   r   r   r&   r&   r&   r'   <module>
   s&    z
+