U
    .e1                     @   s   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 ddlmZ ddl	m
Z
 d dlmZ d dlmZmZ ddlmZmZmZmZmZmZ dd	lmZ d
ddgZG dd
 d
Ze ZG dd deZG dd dZG dd deZdS )    N   )
connection)context)	get_errno)	monotonic)EmptyFull)debugerrorinfoFinalizeregister_after_fork
is_exiting)ForkingPicklerQueueSimpleQueueJoinableQueuec                   @   s   e Zd 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dS ),r   z4
    Queue type using a pipe, buffer and thread
    r   c                 O   s   z|d }W n t k
r(   tdY nX |dkr>ddlm} || _tjdd\| _| _|	 | _
t | _tjdkr~d | _n
|	 | _||| _d| _|   tjdkrt| tj d S )	Nctxz-missing 1 required keyword-only argument: ctxr   r   )SEM_VALUE_MAXFduplexwin32)KeyError	TypeErrorZsynchronizer   _maxsizer   Pipe_reader_writerLock_rlockosgetpid_opidsysplatform_wlockBoundedSemaphore_sem_ignore_epipe_after_forkr   r   selfmaxsizeargskwargsr    r/   3/tmp/pip-unpacked-wheel-39f38r4u/billiard/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/   r0   __getstate__<   s    
   zQueue.__getstate__c              	   C   s0   |\| _ | _| _| _| _| _| _| _|   d S r2   )	r(   r   r   r   r   r%   r'   r"   r)   r+   stater/   r/   r0   __setstate__A   s       zQueue.__setstate__c                 C   sv   t d tt | _t | _d | _d | _	d| _
d| _d | _| jj| _| jj| _| 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_send_bytesr   recvZ_recv
send_bytes
recv_bytes_recv_bytespoll_pollr4   r/   r/   r0   r)   F   s    




zQueue._after_forkTNc              	   C   sZ   | j r
t| j||st| j. | jd kr6|   | j	| | j
  W 5 Q R X d S r2   )rB   AssertionErrorr'   acquirer   r;   r?   _start_threadr>   appendnotifyr+   objblocktimeoutr/   r/   r0   putU   s    

z	Queue.putc              	   C   s   |r2|d kr2| j  |  }W 5 Q R X | j  nv|r@t | }| j ||sRtzH|rz|t  }|dk st| |stn|  st|  }| j  W 5 | j   X t	|S )Nr   )
r   rI   r'   releaser   rM   r   rK   r   loads)r+   rS   rT   resdeadliner/   r/   r0   get`   s&    

z	Queue.getc                 C   s   | j | jj  S r2   )r   r'   _semlock
_get_valuer4   r/   r/   r0   qsizey   s    zQueue.qsizec                 C   s
   |    S r2   rK   r4   r/   r/   r0   empty~   s    zQueue.emptyc                 C   s   | j j S r2   )r'   r[   _is_zeror4   r/   r/   r0   full   s    z
Queue.fullc                 C   s
   |  dS NF)rZ   r4   r/   r/   r0   
get_nowait   s    zQueue.get_nowaitc                 C   s   |  |dS rb   )rU   r+   rR   r/   r/   r0   
put_nowait   s    zQueue.put_nowaitc                 C   s2   d| _ z| j  W 5 | j}|r,d | _|  X d S )NT)rB   rC   r   close)r+   rf   r/   r/   r0   rf      s    zQueue.closec                 C   s$   t d | jst| jr |   d S )NzQueue.join_thread())r	   rB   rL   r@   r4   r/   r/   r0   join_thread   s    
zQueue.join_threadc                 C   s6   t d d| _z| j  W n tk
r0   Y nX d S )NzQueue.cancel_join_thread()T)r	   rA   r@   cancelAttributeErrorr4   r/   r/   r0   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fdd| _d| j_t d | j  t d | jt k}| js|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)targetr-   nameTzdoing self._thread.start()z... done self._thread.start())Zexitpriority
   )r	   r>   clearr9   Threadr   _feedr;   rE   r%   r   rf   r(   r?   daemonstartr"   r    r!   rA   r   _finalize_joinweakrefrefr@   _finalize_closerC   )r+   Zcreated_by_this_processr/   r/   r0   rN      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twrthreadr/   r/   r0   rt      s    
zQueue._finalize_joinc              	   C   s.   t d | | t |  W 5 Q R X d S )Nztelling queue thread to quit)r	   rO   	_sentinelrP   )buffernotemptyr/   r/   r0   rw      s    
zQueue._finalize_closec                 C   sz  t d |j}|j}|j}| j}	t}
tjdkr<|j}|j}nd }z|  z| sT|  W 5 |  X zb|	 }||
krt d |  W W d S t	|}|d kr|| qb|  z|| W 5 |  X qbW qB t
k
r   Y qBX qBW n tk
rt } zx|rt|tjkrW Y \d S z<t r*td|dd n td|ddsJdd l}|  W n tk
rb   Y nX W 5 d }~X Y nX d S )Nz$starting thread to feed data to piper   z%feeder thread got sentinel -- exitingzerror in queue thread: %rT)exc_infor   )r	   rM   rV   waitpopleftrz   r#   r$   r   dumps
IndexError	Exceptionr   errnoEPIPEr   r   r
   	traceback	print_exc)r{   r|   rG   Z	writelockrf   Zignore_epipeZnacquireZnreleaseZnwaitZbpopleftsentinelZwacquireZwreleaserR   excr   r/   r/   r0   rq      sX    




zQueue._feed)r   )TN)TN)__name__
__module____qualname____doc__r1   r5   r8   r)   rU   rZ   r]   r_   ra   rc   re   rf   rg   rj   rN   staticmethodrt   rw   rq   r/   r/   r/   r0   r      s,   



&
	
c                   @   sD   e Zd 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   z
    A queue type which also supports join() and task_done() methods

    Note that if you do not call task_done() for each finished task then
    eventually the counter's semaphore may overflow causing Bad Things
    to happen.
    r   c                 O   sT   z|d }W n t k
r(   tdY nX tj| ||d |d| _| | _d S )Nr   z(missing 1 required keyword argument: ctx)r   r   )r   r   r   r1   	Semaphore_unfinished_tasksr:   _condr*   r/   r/   r0   r1   "  s    zJoinableQueue.__init__c                 C   s   t | | j| jf S r2   )r   r5   r   r   r4   r/   r/   r0   r5   +  s    zJoinableQueue.__getstate__c                 C   s,   t | |d d  |dd  \| _| _d S )N)r   r8   r   r   r6   r/   r/   r0   r8   .  s    zJoinableQueue.__setstate__TNc              
   C   sv   | j r
t| j||st| jJ | j8 | jd kr>|   | j	
| | j  | j  W 5 Q R X W 5 Q R X d S r2   )rB   rL   r'   rM   r   r;   r   r?   rN   r>   rO   r   rV   rP   rQ   r/   r/   r0   rU   2  s    


zJoinableQueue.putc              	   C   s@   | j 0 | jdstd| jj r2| j   W 5 Q R X d S )NFz!task_done() called too many times)r   r   rM   
ValueErrorr[   r`   
notify_allr4   r/   r/   r0   	task_done?  s
    zJoinableQueue.task_donec              	   C   s,   | j  | jj s| j   W 5 Q R X d S r2   )r   r   r[   r`   r~   r4   r/   r/   r0   rx   F  s    zJoinableQueue.join)r   )TN)
r   r   r   r   r1   r5   r8   rU   r   rx   r/   r/   r/   r0   r     s   
	
c                   @   sZ   e Zd Z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S )_SimpleQueuez<
    Simplified Queue type -- really just a locked pipe
    FNc                 C   s2   t jd||d\| _| _| jj| _d  | _| _d S )NF)r   	rnonblock	wnonblock)r   r   r   r   rJ   rK   r   r%   )r+   r   r   r   r/   r/   r0   r1   Q  s      
z_SimpleQueue.__init__c                 C   s
   |    S r2   r^   r4   r/   r/   r0   r_   X  s    z_SimpleQueue.emptyc                 C   s   t |  | j| j| j| jfS r2   )r   r3   r   r   r   r%   r4   r/   r/   r0   r5   [  s    
z_SimpleQueue.__getstate__c                 C   s   |\| _ | _| _| _d S r2   )r   r   r   r%   r6   r/   r/   r0   r8   _  s    z_SimpleQueue.__setstate__c                 C   s
   | j  S r2   )r   rH   r4   r/   r/   r0   get_payloadb  s    z_SimpleQueue.get_payloadc                 C   s   | j | d S r2   )r   rG   r+   valuer/   r/   r0   send_payloade  s    z_SimpleQueue.send_payloadc                 C   s   t |  S r2   )r   rW   r   r4   r/   r/   r0   rZ   h  s    z_SimpleQueue.getc                 C   s   |  t| d S r2   )r   r   r   rd   r/   r/   r0   rU   l  s    z_SimpleQueue.putc                 C   sH   | j d k	r"z| j   W 5 d | _ X | jd k	rDz| j  W 5 d | _X d S r2   )r   rf   r   r4   r/   r/   r0   rf   p  s    

z_SimpleQueue.close)FFN)r   r   r   r   r1   r_   r5   r8   r   r   rZ   rU   rf   r/   r/   r/   r0   r   L  s   
r   c                   @   s$   e Zd Zdd Zdd Zdd ZdS )r   c                 O   sd   z|d }W n t k
r(   tdY nX tjdd\| _| _| | _tj	dkrZ| nd | _
d S )Nr   z&missing required keyword argument: ctxFr   r   )r   r   r   r   r   r   r   r   r#   r$   r%   )r+   r-   r.   r   r/   r/   r0   r1     s    
zSimpleQueue.__init__c              
   C   s(   | j  | j W  5 Q R  S Q R X d S r2   )r   r   rH   r4   r/   r/   r0   r     s    zSimpleQueue.get_payloadc              	   C   s:   | j d kr| j| n| j  | j| W 5 Q R X d S r2   )r%   r   rG   r   r/   r/   r0   r     s    
zSimpleQueue.send_payloadN)r   r   r   r1   r   r   r/   r/   r/   r0   r   ~  s   	) r#   r    r9   r<   ru   r    r   r   compatr   timer   queuer   r   utilr	   r
   r   r   r   r   Z	reductionr   __all__r   objectrz   r   r   r   r/   r/   r/   r0   <module>
   s&    
 x32