a
    ´i÷.  ã                   @   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rŽt| tjƒ d S )Nr   r   )ÚSEM_VALUE_MAXF©ÚduplexÚwin32)Ú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/lib64/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&   r3   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   r5   r<   Ú_start_threadr;   Ú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 ¡  }|  
|¡s¬t	‚n|  
¡ s¬t	‚|  ¡ }| j ¡  W | j ¡  n| j ¡  0 t |¡S rG   )r?   rH   r   rD   r!   ÚreleaseÚtimeÚ	monotonicrI   r   rF   Ú_ForkingPicklerÚloads)r&   rO   rP   ÚresÚ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,   ©rF   r.   r)   r)   r*   Úempty€   s    zQueue.emptyc                 C   s   | j j ¡ S r,   )r!   rZ   Ú_is_zeror.   r)   r)   r*   Úfullƒ   s    z
Queue.fullc                 C   s
   |   d¡S r4   )rY   r.   r)   r)   r*   Ú
get_nowait†   s    zQueue.get_nowaitc                 C   s   |   |d¡S r4   )rQ   ©r&   rN   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*   rd   Œ   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| j| jf	dd�| _d| j_t dƒ | j ¡  t dƒ | js–t| jtjt | j¡gdd�| _t| tj| j| jgd	d�| _d S )
NzQueue._start_thread()ÚQueueFeederThread)ÚtargetÚargsÚnameTzdoing self._thread.start()z... done self._thread.start()éûÿÿÿ)Úexitpriorityé
   )r
   r;   Úclearr7   ÚThreadr   Ú_feedr5   rB   r   r   rd   r   r"   Ú_on_queue_feeder_errorr!   r<   ÚdaemonÚstartr>   r   Ú_finalize_joinÚweakrefÚrefr=   Ú_finalize_closer@   r.   r)   r)   r*   rJ   ¡   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)ÚtwrÚthreadr)   r)   r*   rw   Â   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
   rK   Ú	_sentinelrL   )ÚbufferÚnotemptyr)   r)   r*   rz   Ì   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 r’t 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
   rI   rR   ÚwaitÚpopleftr~   r   r   rU   ÚdumpsÚ
IndexErrorÚ	ExceptionÚgetattrr�   ÚEPIPEr   r   )r   r€   rA   Ú	writelockÚreader_closeÚwriter_closeÚignore_epipeÚonerrorÚ	queue_semÚnacquireÚnreleaseÚnwaitÚbpopleftÚsentinelÚwacquireÚwreleaserN   Úer)   r)   r*   rs   Ó   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–   rN   r—   r)   r)   r*   rt     s    zQueue._on_queue_feeder_error)r   )F)TN)TN)Ú__name__Ú
__module__Ú__qualname__r+   r/   r2   r$   r#   rQ   rY   r\   r^   r`   ra   rc   rd   rf   ri   rJ   Ústaticmethodrw   rz   rs   rt   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_tasksr8   Ú_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   r2   rŸ   rž   r0   r)   r)   r*   r2   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 sœ0    Y  d S rG   )r?   rH   r!   rI   r   r5   rŸ   r<   rJ   r;   rK   rž   rR   rL   rM   r)   r)   r*   rQ   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ž   rI   rH   rZ   r_   Ú
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ž   rZ   r_   r‚   r.   r)   r)   r*   r{   H  s    zJoinableQueue.join)r   )TN)	r™   rš   r›   r+   r/   r2   rQ   r¢   r{   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   rE   rF   r   r   r   )r&   r(   r)   r)   r*   r+   S  s    


zSimpleQueue.__init__c                 C   s   | j  ¡  | j ¡  d S r,   )r   rd   r   r.   r)   r)   r*   rd   \  s    
zSimpleQueue.closec                 C   s
   |   ¡  S r,   r]   r.   r)   r)   r*   r^   `  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   rE   rF   r0   r)   r)   r*   r2   g  s    zSimpleQueue.__setstate__c                 C   s:   | j � | j ¡ }W d   ƒ n1 s&0    Y  t |¡S r,   )r   r   rC   rU   rV   )r&   rW   r)   r)   r*   rY   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,   )rU   r„   r   r   rA   rb   r)   r)   r*   rQ   q  s
    

zSimpleQueue.putN)r™   rš   r›   r+   rd   r^   r/   r2   rY   rQ   ÚclassmethodÚtypesÚGenericAliasÚ__class_getitem__r)   r)   r)   r*   r   Q  s   	
r   )Ú__all__r   r   r7   r9   rS   r¤   rx   r�   Úqueuer   r   Ú_multiprocessingÚ r   r	   Ú	reductionÚForkingPicklerrU   Úutilr
   r   r   r   r   Úobjectr   r~   r   r   r)   r)   r)   r*   Ú<module>
   s&    z
+