B
    ^ cC-                 @   s   d ddg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 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 )-r   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)duplexwin32)Zsynchronizer   _maxsizer   Pipe_reader_writerLock_rlockosgetpid_opidsysplatform_wlockZBoundedSemaphore_sem_ignore_epipe_after_forkr   r   )selfmaxsizectx r$   'lib/python3.7/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 )N)	r   r   r   r   r   r   r   r   r    )r!   stater$   r$   r%   __setstate__>   s    $zQueue.__setstate__c             C   sb   t d tt | _t | _d | _d | _	d| _
d| _d | _| jj| _| jj| _| jj| _d S )NzQueue._after_fork()F)r
   	threading	Conditionr   	_notemptycollectionsdeque_buffer_thread_jointhread_joincancelled_closed_closer   
send_bytes_send_bytesr   
recv_bytes_recv_bytespoll_poll)r!   r$   r$   r%   r    C   s    


zQueue._after_forkTNc          	   C   sd   | j rtd| | j||s&t| j. | jd kr@|   | j	
| | j  W d Q R X d S )NzQueue {0!r} has been closed)r4   AssertionErrorformatr   acquirer   r-   r1   _start_threadr0   appendnotify)r!   objblocktimeoutr$   r$   r%   putP   s    
z	Queue.putc          	   C   s   |r2|d kr2| j  |  }W d Q R X | j  nr|rBt | }| j ||sTtzB|rv|t  }| |stn|  st|  }| j  W d | j   X t	
|S )N)r   r9   r   releasetimeZ	monotonicr>   r   r;   _ForkingPicklerloads)r!   rC   rD   resZdeadliner$   r$   r%   get[   s&    
z	Queue.getc             C   s   | j | jj  S )N)r   r   _semlockZ
_get_value)r!   r$   r$   r%   qsizes   s    zQueue.qsizec             C   s
   |    S )N)r;   )r!   r$   r$   r%   emptyw   s    zQueue.emptyc             C   s   | j j S )N)r   rL   _is_zero)r!   r$   r$   r%   fullz   s    z
Queue.fullc             C   s
   |  dS )NF)rK   )r!   r$   r$   r%   
get_nowait}   s    zQueue.get_nowaitc             C   s   |  |dS )NF)rE   )r!   rB   r$   r$   r%   
put_nowait   s    zQueue.put_nowaitc             C   s2   d| _ z| j  W d | j}|r,d | _|  X d S )NT)r4   r   closer5   )r!   rS   r$   r$   r%   rS      s    zQueue.closec             C   s.   t d | jstd| | jr*|   d S )NzQueue.join_thread()zQueue {0!r} not closed)r
   r4   r<   r=   r2   )r!   r$   r$   r%   join_thread   s    zQueue.join_threadc             C   s6   t d d| _y| j  W n tk
r0   Y nX d S )NzQueue.cancel_join_thread()T)r
   r3   r2   ZcancelAttributeError)r!   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
   r0   clearr+   ZThreadr   _feedr-   r7   r   r   rS   r   _on_queue_feeder_errorr   r1   Zdaemonstartr3   r   _finalize_joinweakrefrefr2   _finalize_closer5   )r!   r$   r$   r%   r?      s*    





zQueue._start_threadc             C   s4   t d |  }|d k	r(|  t d nt d d S )Nzjoining queue threadz... queue thread joinedz... queue thread already dead)r
   join)ZtwrZthreadr$   r$   r%   r`      s    
zQueue._finalize_joinc          	   C   s.   t d | | t |  W d Q R X d S )Nztelling queue thread to quit)r
   r@   	_sentinelrA   )buffernotemptyr$   r$   r%   rc      s    
zQueue._finalize_closec          
   C   sR  t d |j}|j}	|j}
| j}t}tjdkr<|j}|j}nd }x
y|  z| sX|
  W d |	  X ybx\| }||krt d |  d S t	|}|d kr|| qh|  z|| W d |  X qhW W n t
k
r   Y nX W qD tk
rH } zJ|rt|ddtjkrd S t r&td| d S |  ||| W d d }~X Y qDX qDW d S )Nz$starting thread to feed data to piper   z%feeder thread got sentinel -- exitingerrnor   zerror in queue thread: %s)r
   r>   rF   waitpopleftre   r   r   rH   dumps
IndexError	Exceptiongetattrrh   ZEPIPEr   r   )rf   rg   r6   Z	writelockrS   Zignore_epipeonerrorZ	queue_semZnacquireZnreleaseZnwaitZbpopleftsentinelZwacquireZwreleaserB   er$   r$   r%   r]      sR    





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)rq   rB   rr   r$   r$   r%   r^   
  s    zQueue._on_queue_feeder_error)r   )TN)TN)__name__
__module____qualname__r&   r(   r*   r    rE   rK   rM   rN   rP   rQ   rR   rS   rT   rV   r?   staticmethodr`   rc   r]   r^   r$   r$   r$   r%   r   "   s&   



 
>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_tasksr,   _cond)r!   r"   r#   r$   r$   r%   r&      s    zJoinableQueue.__init__c             C   s   t | | j| jf S )N)r   r(   ry   rx   )r!   r$   r$   r%   r(   %  s    zJoinableQueue.__getstate__c             C   s,   t | |d d  |dd  \| _| _d S )N)r   r*   ry   rx   )r!   r)   r$   r$   r%   r*   (  s    zJoinableQueue.__setstate__TNc          
   C   s   | j rtd| | j||s&t| jJ | j8 | jd krH| 	  | j
| | j  | j  W d Q R X W d Q R X d S )NzQueue {0!r} is closed)r4   r<   r=   r   r>   r   r-   ry   r1   r?   r0   r@   rx   rF   rA   )r!   rB   rC   rD   r$   r$   r%   rE   ,  s    

zJoinableQueue.putc          	   C   s@   | j 0 | jdstd| jj r2| j   W d Q R X d S )NFz!task_done() called too many times)ry   rx   r>   
ValueErrorrL   rO   Z
notify_all)r!   r$   r$   r%   	task_done8  s
    zJoinableQueue.task_donec          	   C   s,   | j  | jj s| j   W d Q R X d S )N)ry   rx   rL   rO   ri   )r!   r$   r$   r%   rd   ?  s    zJoinableQueue.join)r   )TN)	rt   ru   rv   r&   r(   r*   rE   r|   rd   r$   r$   r$   r%   r     s   

c               @   s<   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd ZdS )r   c            C   sH   t jdd\| _| _| | _| jj| _tj	dkr:d | _
n
| | _
d S )NF)r   r   )r   r   r   r   r   r   r:   r;   r   r   r   )r!   r#   r$   r$   r%   r&   J  s    


zSimpleQueue.__init__c             C   s
   |    S )N)r;   )r!   r$   r$   r%   rN   S  s    zSimpleQueue.emptyc             C   s   t |  | j| j| j| jfS )N)r	   r'   r   r   r   r   )r!   r$   r$   r%   r(   V  s    
zSimpleQueue.__getstate__c             C   s"   |\| _ | _| _| _| j j| _d S )N)r   r   r   r   r:   r;   )r!   r)   r$   r$   r%   r*   Z  s    zSimpleQueue.__setstate__c          	   C   s&   | j  | j }W d Q R X t|S )N)r   r   r8   rH   rI   )r!   rJ   r$   r$   r%   rK   ^  s    zSimpleQueue.getc          	   C   sD   t |}| jd kr"| j| n| j | j| W d Q R X d S )N)rH   rk   r   r   r6   )r!   rB   r$   r$   r%   rE   d  s
    

zSimpleQueue.putN)	rt   ru   rv   r&   rN   r(   r*   rK   rE   r$   r$   r$   r%   r   H  s   	)__all__r   r   r+   r.   rG   ra   rh   Zqueuer   r   Z_multiprocessing r   r	   Z	reductionZForkingPicklerrH   utilr
   r   r   r   r   objectr   re   r   r   r$   r$   r$   r%   <module>
   s$   
 s
*