
Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­
<!DOCTYPE html>
<html>
3

  \P  ã               @   s^  d Z ddlZddlZddlmZ ddlZddlmZ ddlZddlmZ ddl	m
Z
 ddlZddlZddlmZ ddlZddlZejƒ Zdad	d
„ ZdZG dd„ deƒZG dd„ dƒZdd„ ZG dd„ deƒZG dd„ deƒZG dd„ deƒZdd„ Zdd„ Zdd„ Z dd„ Z!d d!„ Z"da#da$d"d#„ Z%d$d%„ Z&G d&d'„ d'e'ƒZ(G d(d)„ d)ej)ƒZ*ej+eƒ dS )*z"Brian Quinlan (brian@sweetapp.com)é    N)Ú_base)ÚFull)ÚSimpleQueue)Úwait)ÚpartialFc              C   sJ   da ttjƒ ƒ} x| D ]\}}|jd ƒ qW x| D ]\}}|jƒ  q2W d S )NT)Ú	_shutdownÚlistÚ_threads_queuesÚitemsÚputÚjoin)r
   ÚtÚq© r   ú2/usr/lib64/python3.6/concurrent/futures/process.pyÚ_python_exitO   s    r   é   c               @   s   e Zd Zdd„ Zdd„ ZdS )Ú_RemoteTracebackc             C   s
   || _ d S )N)Útb)Úselfr   r   r   r   Ú__init__a   s    z_RemoteTraceback.__init__c             C   s   | j S )N)r   )r   r   r   r   Ú__str__c   s    z_RemoteTraceback.__str__N)Ú__name__Ú
__module__Ú__qualname__r   r   r   r   r   r   r   `   s   r   c               @   s   e Zd Zdd„ Zdd„ ZdS )Ú_ExceptionWithTracebackc             C   s0   t jt|ƒ||ƒ}dj|ƒ}|| _d| | _d S )NÚ z

"""
%s""")Ú	tracebackÚformat_exceptionÚtyper   Úexcr   )r   r    r   r   r   r   r   g   s    
z _ExceptionWithTraceback.__init__c             C   s   t | j| jffS )N)Ú_rebuild_excr    r   )r   r   r   r   Ú
__reduce__l   s    z"_ExceptionWithTraceback.__reduce__N)r   r   r   r   r"   r   r   r   r   r   f   s   r   c             C   s   t |ƒ| _| S )N)r   Ú	__cause__)r    r   r   r   r   r!   o   s    
r!   c               @   s   e Zd Zdd„ ZdS )Ú	_WorkItemc             C   s   || _ || _|| _|| _d S )N)ÚfutureÚfnÚargsÚkwargs)r   r%   r&   r'   r(   r   r   r   r   t   s    z_WorkItem.__init__N)r   r   r   r   r   r   r   r   r$   s   s   r$   c               @   s   e Zd Zddd„ZdS )Ú_ResultItemNc             C   s   || _ || _|| _d S )N)Úwork_idÚ	exceptionÚresult)r   r*   r+   r,   r   r   r   r   {   s    z_ResultItem.__init__)NN)r   r   r   r   r   r   r   r   r)   z   s   r)   c               @   s   e Zd Zdd„ ZdS )Ú	_CallItemc             C   s   || _ || _|| _|| _d S )N)r*   r&   r'   r(   )r   r*   r&   r'   r(   r   r   r   r   �   s    z_CallItem.__init__N)r   r   r   r   r   r   r   r   r-   €   s   r-   c             g   s0   t |Ž }x"ttj|| ƒƒ}|s"d S |V  q
W d S )N)ÚzipÚtupleÚ	itertoolsÚislice)Ú	chunksizeÚ	iterablesÚitÚchunkr   r   r   Ú_get_chunks‡   s    r6   c                s   ‡ fdd„|D ƒS )Nc                s   g | ]}ˆ |Ž ‘qS r   r   )Ú.0r'   )r&   r   r   ú
<listcomp>™   s    z"_process_chunk.<locals>.<listcomp>r   )r&   r5   r   )r&   r   Ú_process_chunk�   s    	r9   c             C   sœ   x–| j dd�}|d kr(|jtjƒ ƒ d S y|j|j|jŽ}W nB tk
r~ } z&t||j	ƒ}|jt
|j|d�ƒ W Y d d }~X qX |jt
|j|d�ƒ qW d S )NT)Úblock)r+   )r,   )Úgetr   ÚosÚgetpidr&   r'   r(   ÚBaseExceptionr   Ú__traceback__r)   r*   )Ú
call_queueÚresult_queueZ	call_itemÚrÚer    r   r   r   Ú_process_worker›   s    &
rD   c             C   sx   xr|j ƒ rd S y|jdd�}W n tjk
r4   d S X | | }|jjƒ rh|jt||j|j	|j
ƒdd� q| |= qqW d S )NF)r:   T)Zfullr;   ÚqueueZEmptyr%   Zset_running_or_notify_cancelr   r-   r&   r'   r(   )Úpending_work_itemsZwork_idsr@   r*   Ú	work_itemr   r   r   Ú_add_call_item_to_queue·   s     

rH   c                s˜  d ‰‡fdd„}‡ ‡fdd„}|j }�xlt||ˆ ƒ dd„ ˆjƒ D ƒ}	t|g|	 ƒ}
||
krf|jƒ }nr| ƒ ‰ˆd k	r„dˆ_dˆ_d ‰x&|jƒ D ]\}}|jj	t
dƒƒ ~qŽW |jƒ  xˆjƒ D ]}|jƒ  q¾W |ƒ  d S t|tƒ�rˆj|ƒ}|jƒ  ˆ�sR|ƒ  d S nJ|d k	�rR|j|jd ƒ}|d k	�rR|j�rB|jj	|jƒ n|jj|jƒ ~| ƒ ‰|ƒ �rŒy|�sr|ƒ  d S W n tk
�rŠ   Y nX d ‰q(W d S )	Nc                  s   t pˆ d kpˆ jS )N)r   Ú_shutdown_threadr   )Úexecutorr   r   Úshutting_downö   s    z/_queue_management_worker.<locals>.shutting_downc                 sZ   t dd„ ˆjƒ D ƒƒ} xtd| ƒD ]}ˆ jd ƒ q"W ˆ jƒ  xˆjƒ D ]}|jƒ  qFW d S )Nc             s   s   | ]}|j ƒ V  qd S )N)Zis_alive)r7   Úpr   r   r   ú	<genexpr>û   s    zD_queue_management_worker.<locals>.shutdown_worker.<locals>.<genexpr>r   )ÚsumÚvaluesÚrangeZ
put_nowaitÚcloser   )Znb_children_aliveÚirL   )r@   Ú	processesr   r   Úshutdown_workerù   s    z1_queue_management_worker.<locals>.shutdown_workerc             S   s   g | ]
}|j ‘qS r   )Úsentinel)r7   rL   r   r   r   r8     s    z,_queue_management_worker.<locals>.<listcomp>Tz^A process in the process pool was terminated abruptly while the future was running or pending.)Z_readerrH   rO   r   ZrecvÚ_brokenrI   r
   r%   Zset_exceptionÚBrokenProcessPoolÚclearZ	terminateÚ
isinstanceÚintÚpopr   r*   r+   Z
set_resultr,   r   )Zexecutor_referencerS   rF   Zwork_ids_queuer@   rA   rK   rT   ÚreaderZ	sentinelsZreadyZresult_itemr*   rG   rL   r   )r@   rJ   rS   r   Ú_queue_management_workerÜ   sb    



r]   c              C   sh   t rtrttƒ‚da ytjdƒ} W n ttfk
r:   d S X | dkrHd S | dkrTd S d|  attƒ‚d S )NTÚSC_SEM_NSEMS_MAXr   é   z@system provides too few semaphores (%d available, 256 necessary)éÿÿÿÿ)Ú_system_limits_checkedÚ_system_limitedÚNotImplementedErrorr<   ÚsysconfÚAttributeErrorÚ
ValueError)Z	nsems_maxr   r   r   Ú_check_system_limitsQ  s    rg   c             c   s.   x(| D ] }|j ƒ  x|r$|jƒ V  qW qW d S )N)Úreverser[   )ÚiterableÚelementr   r   r   Ú_chain_from_iterable_of_listsh  s    
rk   c               @   s   e Zd ZdS )rW   N)r   r   r   r   r   r   r   rW   t  s   rW   c                   sh   e Zd Zddd„Zdd„ Zdd„ Zdd	„ Zejjj	e_	dd
dœ‡ fdd„
Z
ddd„Zejjj	e_	‡  ZS )ÚProcessPoolExecutorNc             C   s’   t ƒ  |d krtjƒ pd| _n|dkr.tdƒ‚|| _tj| jt ƒ| _d| j_	t
ƒ | _tjƒ | _d | _i | _d| _tjƒ | _d| _d| _i | _d S )Nr   r   z"max_workers must be greater than 0TF)rg   r<   Ú	cpu_countÚ_max_workersrf   ÚmultiprocessingZQueueÚEXTRA_QUEUED_CALLSÚ_call_queueZ_ignore_epiper   Ú_result_queuerE   Ú	_work_idsÚ_queue_management_threadÚ
_processesrI   Ú	threadingZLockÚ_shutdown_lockrV   Ú_queue_countÚ_pending_work_items)r   Zmax_workersr   r   r   r   |  s$    


zProcessPoolExecutor.__init__c             C   sp   | j fdd„}| jd krl| jƒ  tjttj| |ƒ| j| j	| j
| j| j fd�| _d| j_| jjƒ  | j t| j< d S )Nc             S   s   |j d ƒ d S )N)r   )Ú_r   r   r   r   Ú
weakref_cb§  s    zFProcessPoolExecutor._start_queue_management_thread.<locals>.weakref_cb)Útargetr'   T)rr   rt   Ú_adjust_process_countrv   ZThreadr]   ÚweakrefÚrefru   ry   rs   rq   ZdaemonÚstartr	   )r   r{   r   r   r   Ú_start_queue_management_thread¤  s    


z2ProcessPoolExecutor._start_queue_management_threadc             C   sJ   xDt t| jƒ| jƒD ].}tjt| j| jfd�}|j	ƒ  || j|j
< qW d S )N)r|   r'   )rP   Úlenru   rn   ro   ZProcessrD   rq   rr   r€   Úpid)r   rz   rL   r   r   r   r}   ¸  s    z)ProcessPoolExecutor._adjust_process_countc             O   s„   | j �t | jrtdƒ‚| jr$tdƒ‚tjƒ }t||||ƒ}|| j| j	< | j
j| j	ƒ |  j	d7  _	| jjd ƒ | jƒ  |S Q R X d S )NzKA child process terminated abruptly, the process pool is not usable anymorez*cannot schedule new futures after shutdownr   )rw   rV   rW   rI   ÚRuntimeErrorr   ZFuturer$   ry   rx   rs   r   rr   r�   )r   r&   r'   r(   ÚfÚwr   r   r   ÚsubmitÁ  s    zProcessPoolExecutor.submitr   )Útimeoutr2   c               s:   |dk rt dƒ‚tƒ jtt|ƒt|d|iŽ|d�}t|ƒS )Nr   zchunksize must be >= 1.r2   )rˆ   )rf   ÚsuperÚmapr   r9   r6   rk   )r   r&   rˆ   r2   r3   Zresults)Ú	__class__r   r   rŠ   Ö  s    zProcessPoolExecutor.mapTc          	   C   sT   | j � d| _W d Q R X | jr8| jjd ƒ |r8| jjƒ  d | _d | _d | _d | _d S )NT)rw   rI   rt   rr   r   r   rq   ru   )r   r   r   r   r   Úshutdownó  s    
zProcessPoolExecutor.shutdown)N)T)r   r   r   r   r�   r}   r‡   r   ÚExecutorÚ__doc__rŠ   rŒ   Ú__classcell__r   r   )r‹   r   rl   {  s   
(	
rl   ),Ú
__author__Úatexitr<   Zconcurrent.futuresr   rE   r   ro   r   Zmultiprocessing.connectionr   rv   r~   Ú	functoolsr   r0   r   ÚWeakKeyDictionaryr	   r   r   rp   Ú	Exceptionr   r   r!   Úobjectr$   r)   r-   r6   r9   rD   rH   r]   ra   rb   rg   rk   r„   rW   r�   rl   Úregisterr   r   r   r   Ú<module>.   sH   		%s 	