o
    éT•j÷.  ã                   @   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d/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d#d$„ Zed%d&„ ƒZed'd(„ ƒZed)d*„ ƒZed+d,„ ƒZdS )0r   r   c                C   s–   |dkr
ddl m} || _tjdd�\| _| _| ¡ | _t	 
¡ | _tjdkr*d | _n| ¡ | _| |¡| _d| _|  ¡  tjdkrIt| tjƒ d S 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/lib/python3.10/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   s|   | j rtd| ›d�ƒ‚| j ||¡st‚| j� | jd u r!|  ¡  | j 	|¡ | j 
¡  W d   ƒ d S 1 s7w   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 s#w   Y  | j ¡  n>|r6t ¡ | }| j ||¡s?t	‚z&|rP|t ¡  }|  
|¡sOt	‚n|  
¡ sVt	‚|  ¡ }| j ¡  W | j ¡  n| j ¡  w 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 r,   ©rF   r.   r)   r)   r*   Úempty€   ó   
zQueue.emptyc                 C   s   | j j ¡ S r,   )r!   rZ   Ú_is_zeror.   r)   r)   r*   Úfullƒ   ó   z
Queue.fullc                 C   s
   |   d¡S r4   )rY   r.   r)   r)   r*   Ú
get_nowait†   r`   zQueue.get_nowaitc                 C   s   |   |d¡S r4   )rQ   ©r&   rN   r)   r)   r*   Ú
put_nowait‰   rc   zQueue.put_nowaitc                 C   s$   d| _ | j}|rd | _|ƒ  d S d S )NT)r?   r@   )r&   Úcloser)   r)   r*   rg   Œ   s   
þzQueue.closec                 C   s2   t dƒ | jsJ d | ¡ƒ‚| jr|  ¡  d S 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 d S  ty   Y d S w )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sKt| 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   rg   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   s6   t dƒ | ƒ }|d ur| ¡  t dƒ d S t dƒ d S )Nzjoining queue threadz... queue thread joinedz... queue thread already dead)r
   Újoin)ÚtwrÚthreadr)   r)   r*   rz   Â   s   zQueue._finalize_joinc                 C   sD   t dƒ |� |  t¡ | ¡  W d   ƒ d S 1 sw   Y  d S )Nztelling queue thread to quit)r
   rK   Ú	_sentinelrL   )ÚbufferÚnotemptyr)   r)   r*   r}   Ì   s
   

"þzQueue._finalize_closec	              
   C   sf  t dƒ |j}	|j}
|j}| j}t}tjdkr|j}|j}nd }	 zT|	ƒ  z
| s+|ƒ  W |
ƒ  n|
ƒ  w z6	 |ƒ }||u rKt dƒ |ƒ  |ƒ  W W d S t 	|¡}|d u rY||ƒ n|ƒ  z	||ƒ W |ƒ  n|ƒ  w q6 t
ys   Y nw W n< ty± } z0|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 }~nd }~ww q!)Nz$starting thread to feed data to piper   r   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*   rv   Ó   sb   
€


ïÿ
€ïã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*   rw     s   zQueue._on_queue_feeder_error©r   )F©TN)Ú__name__Ú
__module__Ú__qualname__r+   r/   r2   r$   r#   rQ   rY   r\   r_   rb   rd   rf   rg   ri   rl   rJ   Ústaticmethodrz   r}   rv   rw   r)   r)   r)   r*   r   #   s2    



!
	

>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�= | j�! | jd u r%|  ¡  | j	 
|¡ | j ¡  | j ¡  W d   ƒ n1 s?w   Y  W d   ƒ d S W d   ƒ d S 1 sWw   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   

PûzJoinableQueue.putc                 C   sf   | j �& | j d¡stdƒ‚| jj ¡ r!| j  ¡  W d   ƒ d S W d   ƒ d S 1 s,w   Y  d S )NFz!task_done() called too many times)r¤   r£   rI   rH   rZ   ra   Ú
notify_allr.   r)   r)   r*   Ú	task_doneA  s   ü"ýzJoinableQueue.task_donec                 C   sR   | j � | jj ¡ s| j  ¡  W d   ƒ d S W d   ƒ d S 1 s"w   Y  d S r,   )r¤   r£   rZ   ra   r…   r.   r)   r)   r*   r~   H  s   þ"ÿzJoinableQueue.joinrœ   r�   )	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   sJ   t jdd�\| _| _| ¡ | _| jj| _tj	dkrd | _
d S | ¡ | _
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   rg   r   r.   r)   r)   r*   rg   \  s   
zSimpleQueue.closec                 C   r]   r,   r^   r.   r)   r)   r*   r_   `  r`   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w   Y  t |¡S r,   )r   r   rC   rU   rV   )r&   rW   r)   r)   r*   rY   k  s   ÿ
zSimpleQueue.getc                 C   s\   t  |¡}| jd u r| j |¡ d S | j� | j |¡ W d   ƒ d S 1 s'w   Y  d S r,   )rU   r‡   r   r   rA   re   r)   r)   r*   rQ   q  s   

"ÿzSimpleQueue.putN)rž   rŸ   r    r+   rg   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©   r{   r„   Úqueuer   r   Ú_multiprocessingÚ r   r	   Ú	reductionÚForkingPicklerrU   Úutilr
   r   r   r   r   Úobjectr   r�   r   r   r)   r)   r)   r*   Ú<module>   s(   	 z
+