U
    .d                     @   s   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m	Z
mZmZ d dlmZ ddlmZ ddd	gZG d
d de
Z	G dd deZdS )    N)util)FullQueueSimpleQueue	_sentinel)assert_spawning   )dumpsr   r   r   c                       sJ   e Zd Zd fdd	Zdd Zdd Zd	d
 Zedd Zdd Z	  Z
S )r   r   Nc                    s   t  j||d || _d S )N)maxsizectxsuper__init__	_reducers)selfr
   reducersr   	__class__ H/tmp/pip-unpacked-wheel-ho8k9mhl/joblib/externals/loky/backend/queues.pyr       s    zQueue.__init__c              	   C   s0   t |  | j| j| j| j| j| j| j| j| j	f	S N)
r   _ignore_epipe_maxsize_reader_writerr   _rlock_wlock_sem_opidr   r   r   r   __getstate__%   s       zQueue.__getstate__c              
   C   sH   |\	| _ | _| _| _| _| _| _| _| _t	j
dkr<|   n|   d S )N)   	   )r   r   r   r   r   r   r   r   r   sysversion_infoZ_reset_after_forkr   stater   r   r   __setstate__+   s       

zQueue.__setstate__c                 C   s   t d | j  tjtj| j| j| 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| jtjt| jgdd| _t j| 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   debug_bufferclear	threadingThreadr   _feedZ	_notemptyZ_send_bytesr   r   closer   r   _on_queue_feeder_errorr   _threaddaemonstartr   osgetpidZ_joincancelledZFinalizeZ_finalize_joinweakrefrefZ_jointhreadZ_finalize_close_close)r   Zcreated_by_this_processr   r   r   _start_thread5   s@    

    



  
zQueue._start_threadc	              
   C   sj  t d |j}	|j}
|j}| j}t}tjdkr>|j}|j}nd }z|	  z| sV|  W 5 |
  X zj| }||krt d |  W W d S t	||d}|d kr|| n|  z|| W 5 |  X ~~qdW n t
k
r   Y nX W qB tk
rb } z^|rt|ddtjkrW Y >d S t  r@t d|  W Y d S |  ||| W 5 d }~X Y qBX qBd S )Nz$starting thread to feed data to pipewin32z%feeder thread got sentinel -- exitingr   errnor   zerror in queue thread: )r   r.   acquirereleasewaitpopleftr   r#   platformr	   
IndexErrorBaseExceptiongetattrrA   EPIPEZ
is_exitinginfo)bufferZnotempty
send_bytesZ	writelockr4   r   Zignore_epipeonerrorZ	queue_semZnacquireZnreleaseZnwaitZbpopleftsentinelZwacquireZwreleaseobjZobj_er   r   r   r3   ]   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   rQ   rP   rR   r   r   r   r5      s    zQueue._on_queue_feeder_error)r   NN)__name__
__module____qualname__r   r    r(   r>   staticmethodr3   r5   __classcell__r   r   r   r   r      s   
(
:c                       s>   e Zd Zd fdd	Zdd Zdd Zdd	 Zd
d Z  ZS )r   Nc                    s   t  j|d || _d S )N)r   r   )r   r   r   r   r   r   r      s    zSimpleQueue.__init__c                 C   s   | j   | j  d S r   )r   r4   r   r   r   r   r   r4      s    
zSimpleQueue.closec                 C   s    t |  | j| j| j| j| jfS r   )r   r   r   r   r   r   r   r   r   r   r       s    zSimpleQueue.__getstate__c                 C   s   |\| _ | _| _| _| _d S r   )r   r   r   r   r   r&   r   r   r   r(      s    zSimpleQueue.__setstate__c              	   C   sH   t || jd}| jd kr&| j| n| j | j| W 5 Q R X d S )Nr@   )r	   r   r   r   rM   )r   rP   r   r   r   put   s
    
zSimpleQueue.put)NN)	rT   rU   rV   r   r4   r    r(   rY   rX   r   r   r   r   r      s
   )r9   r#   rA   r;   r1   multiprocessingr   Zmultiprocessing.queuesr   r   Zmp_Queuer   Zmp_SimpleQueuer   Zmultiprocessing.contextr   Z	reductionr	   __all__r   r   r   r   <module>
   s   
 