a
   )                   @   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_wlockBoundedSemaphore_sem_ignore_epipe_resetr   r   _after_forkselfmaxsizectx r'   ,/usr/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$   r1   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   r3   r:   _start_threadr9   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 rE   )r=   rF   r   rB   r   releasetime	monotonicrG   r   rD   _ForkingPicklerloads)r$   rM   rN   resZdeadliner'   r'   r(   getb   s*    &
z	Queue.getc                 C   s   | j | jj  S r*   )r   r   _semlock
_get_valuer,   r'   r'   r(   qsize|   s    zQueue.qsizec                 C   s
   |    S r*   rD   r,   r'   r'   r(   empty   s    zQueue.emptyc                 C   s   | j j S r*   )r   rW   _is_zeror,   r'   r'   r(   full   s    z
Queue.fullc                 C   s
   |  dS r2   )rV   r,   r'   r'   r(   
get_nowait   s    zQueue.get_nowaitc                 C   s   |  |dS r2   )rO   r$   rL   r'   r'   r(   
put_nowait   s    zQueue.put_nowaitc                 C   sH   d| _ z$| j  W | j}|rDd | _|  n| j}|rBd | _|  0 d S )NT)r=   r   closer>   )r$   ra   r'   r'   r(   ra      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;   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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
   r9   clearr5   Threadr   _feedr3   r@   r   r   ra   r    _on_queue_feeder_errorr   r:   daemonstartr<   r   _finalize_joinweakrefrefr;   _finalize_closer>   r,   r'   r'   r(   rH      s2    


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(   rr      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
   rI   	_sentinelrJ   )buffernotemptyr'   r'   r(   ru      s    
zQueue._finalize_closec              
   C   sl  t d |j}|j}	|j}
| j}t}tjdkr<|j}|j}nd }z|  z| sT|
  W |	  n|	  0 zh| }||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yd } zf|rt|ddtjkrW Y d }~d S t r>t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
   rG   rP   waitpopleftrx   r   r   rS   dumps
IndexError	Exceptiongetattrr{   ZEPIPEr   r   )ry   rz   r?   Z	writelockra   Zignore_epipeonerrorZ	queue_semZnacquireZnreleaseZnwaitZbpopleftsentinelZwacquireZwreleaserL   er'   r'   r(   rn      sN    





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   rL   r   r'   r'   r(   ro     s    zQueue._on_queue_feeder_error)r   )F)TN)TN)__name__
__module____qualname__r)   r-   r0   r"   r!   rO   rV   rY   r[   r]   r^   r`   ra   rc   rf   rH   staticmethodrr   ru   rn   ro   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)   	Semaphore_unfinished_tasksr6   _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   r0   r   r   r.   r'   r'   r(   r0   1  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 rE   )r=   rF   r   rG   r   r3   r   r:   rH   r9   rI   r   rP   rJ   rK   r'   r'   r(   rO   5  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   rG   rF   rW   r\   
notify_allr,   r'   r'   r(   	task_doneB  s
    zJoinableQueue.task_donec                 C   s@   | j & | jj s| j   W d    n1 s20    Y  d S r*   )r   r   rW   r\   r|   r,   r'   r'   r(   rv   I  s    zJoinableQueue.join)r   )TN)	r   r   r   r)   r-   r0   rO   r   rv   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   rC   rD   r   r   r   )r$   r&   r'   r'   r(   r)   T  s    


zSimpleQueue.__init__c                 C   s   | j   | j  d S r*   )r   ra   r   r,   r'   r'   r(   ra   ]  s    
zSimpleQueue.closec                 C   s
   |    S r*   rZ   r,   r'   r'   r(   r[   a  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-   d  s    
zSimpleQueue.__getstate__c                 C   s"   |\| _ | _| _| _| j j| _d S r*   )r   r   r   r   rC   rD   r.   r'   r'   r(   r0   h  s    zSimpleQueue.__setstate__c                 C   s:   | j  | j }W d    n1 s&0    Y  t|S r*   )r   r   rA   rS   rT   )r$   rU   r'   r'   r(   rV   l  s    (zSimpleQueue.getc                 C   sX   t |}| jd u r"| j| n2| j | j| W d    n1 sJ0    Y  d S r*   )rS   r~   r   r   r?   r_   r'   r'   r(   rO   r  s
    

zSimpleQueue.putN)r   r   r   r)   ra   r[   r-   r0   rV   rO   classmethodtypesGenericAlias__class_getitem__r'   r'   r'   r(   r   R  s   	
r   )__all__r   r   r5   r7   rQ   r   rs   r{   Zqueuer   r   Z_multiprocessing r   r	   Z	reductionZForkingPicklerrS   utilr
   r   r   r   r   objectr   rx   r   r   r'   r'   r'   r(   <module>
   s&    {
+