a
    ´i£z  ã                   @   s~  d Z ddlZddlmZ ddlZddlZddl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ddlZe ¡ ZdaG dd„ dƒZd	d
„ Ze
 e¡ 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 G dd„ de	ƒZ!dd„ Z"dd„ Z#d-dd „Z$d!d"„ Z%G d#d$„ d$e
j&ƒZ'da(da)d%d&„ Z*d'd(„ Z+G d)d*„ d*ej,ƒZ-G d+d,„ d,ej.ƒZ/dS ).z"Brian Quinlan (brian@sweetapp.com)é    N)Ú_base)ÚQueue)ÚpartialFc                   @   s,   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	S )
Ú_ThreadWakeupc                 C   s   d| _ tjdd�\| _| _d S )NF)Úduplex)Ú_closedÚmpÚPipeÚ_readerÚ_writer©Úself© r   ú2/usr/lib64/python3.9/concurrent/futures/process.pyÚ__init__C   s    z_ThreadWakeup.__init__c                 C   s$   | j s d| _ | j ¡  | j ¡  d S ©NT)r   r   Úcloser
   r   r   r   r   r   G   s    
z_ThreadWakeup.closec                 C   s   | j s| j d¡ d S )Nó    )r   r   Ú
send_bytesr   r   r   r   ÚwakeupM   s    z_ThreadWakeup.wakeupc                 C   s    | j s| j ¡ r| j ¡  qd S ©N)r   r
   ÚpollÚ
recv_bytesr   r   r   r   ÚclearQ   s    
z_ThreadWakeup.clearN)Ú__name__Ú
__module__Ú__qualname__r   r   r   r   r   r   r   r   r   B   s   r   c                  C   s@   da tt ¡ ƒ} | D ]\}}| ¡  q| D ]\}}| ¡  q*d S r   )Ú_global_shutdownÚlistÚ_threads_wakeupsÚitemsr   Újoin)r    Ú_Úthread_wakeupÚtr   r   r   Ú_python_exitW   s    
r%   é   é=   c                   @   s   e Zd Zdd„ Zdd„ ZdS )Ú_RemoteTracebackc                 C   s
   || _ d S r   ©Útb)r   r*   r   r   r   r   w   s    z_RemoteTraceback.__init__c                 C   s   | j S r   r)   r   r   r   r   Ú__str__y   s    z_RemoteTraceback.__str__N)r   r   r   r   r+   r   r   r   r   r(   v   s   r(   c                   @   s   e Zd Zdd„ Zdd„ ZdS )Ú_ExceptionWithTracebackc                 C   s8   t  t|ƒ||¡}d |¡}|| _d | j_d| | _d S )NÚ z

"""
%s""")Ú	tracebackÚformat_exceptionÚtyper!   ÚexcÚ__traceback__r*   )r   r1   r*   r   r   r   r   }   s
    
z _ExceptionWithTraceback.__init__c                 C   s   t | j| jffS r   )Ú_rebuild_excr1   r*   r   r   r   r   Ú
__reduce__…   s    z"_ExceptionWithTraceback.__reduce__N)r   r   r   r   r4   r   r   r   r   r,   |   s   r,   c                 C   s   t |ƒ| _| S r   )r(   Ú	__cause__)r1   r*   r   r   r   r3   ˆ   s    
r3   c                   @   s   e Zd Zdd„ ZdS )Ú	_WorkItemc                 C   s   || _ || _|| _|| _d S r   )ÚfutureÚfnÚargsÚkwargs)r   r7   r8   r9   r:   r   r   r   r   �   s    z_WorkItem.__init__N©r   r   r   r   r   r   r   r   r6   Œ   s   r6   c                   @   s   e Zd Zddd„ZdS )Ú_ResultItemNc                 C   s   || _ || _|| _d S r   )Úwork_idÚ	exceptionÚresult)r   r=   r>   r?   r   r   r   r   ”   s    z_ResultItem.__init__)NNr;   r   r   r   r   r<   “   s   r<   c                   @   s   e Zd Zdd„ ZdS )Ú	_CallItemc                 C   s   || _ || _|| _|| _d S r   )r=   r8   r9   r:   )r   r=   r8   r9   r:   r   r   r   r   š   s    z_CallItem.__init__Nr;   r   r   r   r   r@   ™   s   r@   c                       s*   e Zd Zd‡ fdd„	Z‡ fdd„Z‡  ZS )Ú
_SafeQueuer   c                   s&   || _ || _|| _tƒ j||d� d S )N)Úctx)Úpending_work_itemsÚshutdown_lockr#   Úsuperr   )r   Úmax_sizerB   rC   rD   r#   ©Ú	__class__r   r   r   £   s    z_SafeQueue.__init__c                    sœ   t |tƒrŠt t|ƒ||j¡}td d |¡¡ƒ|_	| j
 |jd ¡}| j� | j ¡  W d   ƒ n1 sj0    Y  |d ur˜|j |¡ ntƒ  ||¡ d S )Nz

"""
{}"""r-   )Ú
isinstancer@   r.   r/   r0   r2   r(   Úformatr!   r5   rC   Úpopr=   rD   r#   r   r7   Úset_exceptionrE   Ú_on_queue_feeder_error)r   ÚeÚobjr*   Ú	work_itemrG   r   r   rM   ª   s    
(z!_SafeQueue._on_queue_feeder_error)r   )r   r   r   r   rM   Ú__classcell__r   r   rG   r   rA   ¡   s   rA   c                 g   s,   t |Ž }tt || ¡ƒ}|s d S |V  qd S r   )ÚzipÚtupleÚ	itertoolsÚislice)Ú	chunksizeÚ	iterablesÚitÚchunkr   r   r   Ú_get_chunksº   s
    rZ   c                    s   ‡ fdd„|D ƒS )Nc                    s   g | ]}ˆ |Ž ‘qS r   r   )Ú.0r9   ©r8   r   r   Ú
<listcomp>Í   r   z"_process_chunk.<locals>.<listcomp>r   )r8   rY   r   r\   r   Ú_process_chunkÄ   s    	r^   c              
   C   s`   z|   t|||d�¡ W nB tyZ } z*t||jƒ}|   t||d�¡ W Y d }~n
d }~0 0 d S )N)r?   r>   ©r>   )Úputr<   ÚBaseExceptionr,   r2   )Úresult_queuer=   r?   r>   rN   r1   r   r   r   Ú_sendback_resultÐ   s    
ÿrc   c              
   C   sÔ   |d ur:z||Ž  W n$ t y8   tjjddd� Y d S 0 | jdd�}|d u r`| t ¡ ¡ d S z|j|j	i |j
¤Ž}W n@ t y¸ } z(t||jƒ}t||j|d� W Y d }~nd }~0 0 t||j|d� ~~q:d S )NzException in initializer:T)Úexc_info©Úblockr_   )r?   )ra   r   ÚLOGGERÚcriticalÚgetr`   ÚosÚgetpidr8   r9   r:   r,   r2   rc   r=   )Ú
call_queuerb   ÚinitializerÚinitargsÚ	call_itemÚrrN   r1   r   r   r   Ú_process_workerÚ   s$    &rq   c                       sl   e Zd Z‡ f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dd„ Z‡  ZS )Ú_ExecutorManagerThreadc                    sf   |j | _|j| _| j| jfdd„}t ||¡| _|j| _|j	| _
|j| _|j| _|j| _tƒ  ¡  d S )Nc                 S   s<   t j d¡ |� | ¡  W d   ƒ n1 s.0    Y  d S )Nz?Executor collected: triggering callback for QueueManager wakeup)r   ÚutilÚdebugr   )r"   r#   rD   r   r   r   Ú
weakref_cb  s    z3_ExecutorManagerThread.__init__.<locals>.weakref_cb)Ú_executor_manager_thread_wakeupr#   Ú_shutdown_lockrD   ÚweakrefÚrefÚexecutor_referenceÚ
_processesÚ	processesÚ_call_queuerl   Ú_result_queuerb   Ú	_work_idsÚwork_ids_queueÚ_pending_work_itemsrC   rE   r   )r   Úexecutorru   rG   r   r   r     s    	þ
z_ExecutorManagerThread.__init__c                 C   s€   |   ¡  |  ¡ \}}}|r(|  |¡ d S |d urX|  |¡ ~|  ¡ }|d urV|j ¡  ~|  ¡ r |  ¡  | j	s |  
¡  d S q d S r   )Úadd_call_item_to_queueÚwait_result_broken_or_wakeupÚterminate_brokenÚprocess_result_itemrz   Ú_idle_worker_semaphoreÚreleaseÚis_shutting_downÚflag_executor_shutting_downrC   Újoin_executor_internals)r   Úresult_itemÚ	is_brokenÚcauser‚   r   r   r   Úrun:  s"    


z_ExecutorManagerThread.runc                 C   s~   | j  ¡ rd S z| jjdd�}W n tjy6   Y d S 0 | j| }|j ¡ rn| j j	t
||j|j|jƒdd� q | j|= q q d S )NFre   T)rl   Úfullr€   ri   ÚqueueÚEmptyrC   r7   Úset_running_or_notify_cancelr`   r@   r8   r9   r:   )r   r=   rP   r   r   r   rƒ   Z  s"    



ýüz-_ExecutorManagerThread.add_call_item_to_queuec           
   
   C   sæ   | j j}| jj}||g}dd„ t| j ¡ ƒD ƒ}tj || ¡}d }d}d }||v r z| 	¡ }d}W q¬ t
yœ }	 z t t|	ƒ|	|	j¡}W Y d }	~	q¬d }	~	0 0 n||v r¬d}| j� | j ¡  W d   ƒ n1 sÒ0    Y  |||fS )Nc                 S   s   g | ]
}|j ‘qS r   )Úsentinel©r[   Úpr   r   r   r]   {  r   zG_ExecutorManagerThread.wait_result_broken_or_wakeup.<locals>.<listcomp>TF)rb   r
   r#   r   r|   Úvaluesr   Ú
connectionÚwaitÚrecvra   r.   r/   r0   r2   rD   r   )
r   Úresult_readerÚwakeup_readerÚreadersÚworker_sentinelsÚreadyrŽ   r�   rŒ   rN   r   r   r   r„   q  s&    ,(z3_ExecutorManagerThread.wait_result_broken_or_wakeupc                 C   sr   t |tƒr2| j |¡}| ¡  | jsn|  ¡  d S n<| j |jd ¡}|d urn|jr`|j	 
|j¡ n|j	 |j¡ d S r   )rI   Úintr|   rK   r!   r‹   rC   r=   r>   r7   rL   Ú
set_resultr?   )r   rŒ   r–   rP   r   r   r   r†   �  s    
z*_ExecutorManagerThread.process_result_itemc                 C   s   |   ¡ }tp|d u p|jS r   )rz   r   Ú_shutdown_thread)r   r‚   r   r   r   r‰   §  s    ÿz'_ExecutorManagerThread.is_shutting_downc                 C   s˜   |   ¡ }|d ur d|_d|_d }tdƒ}|d urHtdd |¡› d�ƒ|_| j ¡ D ]\}}|j	 
|¡ ~qR| j ¡  | j ¡ D ]}| ¡  q~|  ¡  d S )NzKA child process terminated abruptly, the process pool is not usable anymoreTz^A process in the process pool was terminated abruptly while the future was running or pending.z
'''
r-   z''')rz   Ú_brokenr¢   ÚBrokenProcessPoolr(   r!   r5   rC   r    r7   rL   r   r|   r—   Ú	terminater‹   )r   rŽ   r‚   Úbper=   rP   r–   r   r   r   r…   ±  s"    ÿ

z'_ExecutorManagerThread.terminate_brokenc                 C   s€   |   ¡ }|d ur|d|_|jr|i }| j ¡ D ]\}}|j ¡ s*|||< q*|| _z| j ¡  W qL t	j
yr   Y qvY qL0 qLd|_d S )NTF)rz   r¢   Ú_cancel_pending_futuresrC   r    r7   Úcancelr€   Ú
get_nowaitr‘   r’   )r   r‚   Únew_pending_work_itemsr=   rP   r   r   r   rŠ   ×  s    

z2_ExecutorManagerThread.flag_executor_shutting_downc              	   C   sl   |   ¡ }d}||k rh|   ¡ dkrht|| ƒD ]8}z| j d ¡ |d7 }W q, tjyb   Y  qY q,0 q,qd S )Nr   r&   )Úget_n_children_aliveÚrangerl   Ú
put_nowaitr‘   ÚFull)r   Ún_children_to_stopÚn_sentinels_sentÚir   r   r   Úshutdown_workersñ  s    
ÿz'_ExecutorManagerThread.shutdown_workersc                 C   sh   |   ¡  | j ¡  | j ¡  | j� | j ¡  W d   ƒ n1 sB0    Y  | j ¡ D ]}| ¡  qVd S r   )	r²   rl   r   Újoin_threadrD   r#   r|   r—   r!   ©r   r–   r   r   r   r‹   ÿ  s    

(z._ExecutorManagerThread.join_executor_internalsc                 C   s   t dd„ | j ¡ D ƒƒS )Nc                 s   s   | ]}|  ¡ V  qd S r   )Úis_aliver•   r   r   r   Ú	<genexpr>  r   z>_ExecutorManagerThread.get_n_children_alive.<locals>.<genexpr>)Úsumr|   r—   r   r   r   r   r«     s    z+_ExecutorManagerThread.get_n_children_alive)r   r   r   r   r�   rƒ   r„   r†   r‰   r…   rŠ   r²   r‹   r«   rQ   r   r   rG   r   rr     s   + 
&rr   c               	   C   sh   t rtrttƒ‚da zt d¡} W n ttfy:   Y d S 0 | dkrHd S | dkrTd S d|  attƒ‚d S )NTÚSC_SEM_NSEMS_MAXéÿÿÿÿé   z@system provides too few semaphores (%d available, 256 necessary))Ú_system_limits_checkedÚ_system_limitedÚNotImplementedErrorrj   ÚsysconfÚAttributeErrorÚ
ValueError)Ú	nsems_maxr   r   r   Ú_check_system_limits  s     ÿrÂ   c                 c   s&   | D ]}|  ¡  |r| ¡ V  qqd S r   )ÚreverserK   )ÚiterableÚelementr   r   r   Ú_chain_from_iterable_of_lists,  s    rÆ   c                   @   s   e Zd ZdS )r¤   N)r   r   r   r   r   r   r   r¤   8  s   r¤   c                       s~   e Zd Zddd„Zdd„ Z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œdd„Ze	j
jje_‡  ZS )ÚProcessPoolExecutorNr   c                 C   sJ  t ƒ  |d u r6t ¡ pd| _tjdkrntt| jƒ| _n8|dkrHtdƒ‚n tjdkrh|tkrhtdt› �ƒ‚|| _|d u r~t	 
¡ }|| _| jjdd�dk| _|d ur°t|ƒs°td	ƒ‚|| _|| _d | _i | _d| _t ¡ | _t d¡| _d| _d| _i | _d| _tƒ | _| jt  }t!|| j| j| j| jd
�| _"d| j"_#| $¡ | _%t& '¡ | _(d S )Nr&   Úwin32r   z"max_workers must be greater than 0zmax_workers must be <= F)Ú
allow_noneÚforkzinitializer must be a callable)rF   rB   rC   rD   r#   T))rÂ   rj   Ú	cpu_countÚ_max_workersÚsysÚplatformÚminÚ_MAX_WINDOWS_WORKERSrÀ   r   Úget_contextÚ_mp_contextÚget_start_methodÚ#_safe_to_dynamically_spawn_childrenÚcallableÚ	TypeErrorÚ_initializerÚ	_initargsÚ_executor_manager_threadr{   r¢   Ú	threadingÚLockrw   Ú	Semaphorer‡   r£   Ú_queue_countr�   r§   r   rv   ÚEXTRA_QUEUED_CALLSrA   r}   Ú_ignore_epipeÚSimpleQueuer~   r‘   r   r   )r   Úmax_workersÚ
mp_contextrm   rn   Ú
queue_sizer   r   r   r   @  sZ    
ÿ

ÿÿÿ


ü
zProcessPoolExecutor.__init__c                 C   s<   | j d u r8| js|  ¡  t| ƒ| _ | j  ¡  | jt| j < d S r   )rÙ   rÔ   Ú_launch_processesrr   Ústartrv   r   r   r   r   r   Ú_start_executor_manager_thread•  s    


ÿz2ProcessPoolExecutor._start_executor_manager_threadc                 C   s2   | j jdd�rd S t| jƒ}|| jk r.|  ¡  d S )NF)Úblocking)r‡   ÚacquireÚlenr{   rÌ   Ú_spawn_process)r   Úprocess_countr   r   r   Ú_adjust_process_countŸ  s
    

z)ProcessPoolExecutor._adjust_process_countc                 C   s$   t t| jƒ| jƒD ]}|  ¡  qd S r   )r¬   ré   r{   rÌ   rê   )r   r"   r   r   r   rä   ®  s    z%ProcessPoolExecutor._launch_processesc                 C   s8   | j jt| j| j| j| jfd�}| ¡  || j|j	< d S )N)Útargetr9   )
rÒ   ÚProcessrq   r}   r~   r×   rØ   rå   r{   Úpidr´   r   r   r   rê   ¶  s    ýþz"ProcessPoolExecutor._spawn_processc                O   s¶   | j �œ | jrt| jƒ‚| jr&tdƒ‚tr2tdƒ‚t ¡ }t||||ƒ}|| j	| j
< | j | j
¡ |  j
d7  _
| j ¡  | jrˆ|  ¡  |  ¡  |W  d   ƒ S 1 s¨0    Y  d S )Nz*cannot schedule new futures after shutdownz6cannot schedule new futures after interpreter shutdownr&   )rw   r£   r¤   r¢   ÚRuntimeErrorr   r   ÚFuturer6   r�   rÝ   r   r`   rv   r   rÔ   rì   ræ   )r   r8   r9   r:   ÚfÚwr   r   r   ÚsubmitÀ  s"    

zProcessPoolExecutor.submitr&   )ÚtimeoutrV   c                   s:   |dk rt dƒ‚tƒ jtt|ƒt|d|iŽ|d�}t|ƒS )Nr&   zchunksize must be >= 1.rV   )rõ   )rÀ   rE   Úmapr   r^   rZ   rÆ   )r   r8   rõ   rV   rW   ÚresultsrG   r   r   rö   Ù  s    þzProcessPoolExecutor.mapTF)Úcancel_futuresc                C   s˜   | j �0 || _d| _| jd ur(| j ¡  W d   ƒ n1 s<0    Y  | jd ur^|r^| j ¡  d | _d | _| jd ur‚|r‚| j 	¡  d | _d | _
d | _d S r   )rw   r§   r¢   rv   r   rÙ   r!   r}   r~   r   r{   )r   r™   rø   r   r   r   Úshutdownö  s    
(

zProcessPoolExecutor.shutdown)NNNr   )T)r   r   r   r   ræ   rì   rä   rê   rô   r   ÚExecutorÚ__doc__rö   rù   rQ   r   r   rG   r   rÇ   ?  s     ÿ
U

rÇ   )NN)0Ú
__author__rj   Úconcurrent.futuresr   r‘   Úmultiprocessingr   Úmultiprocessing.connectionÚmultiprocessing.queuesr   rÚ   rx   Ú	functoolsr   rT   rÍ   r.   ÚWeakKeyDictionaryr   r   r   r%   Ú_register_atexitrÞ   rÐ   Ú	Exceptionr(   r,   r3   Úobjectr6   r<   r@   rA   rZ   r^   rc   rq   ÚThreadrr   r»   r¼   rÂ   rÆ   ÚBrokenExecutorr¤   rú   rÇ   r   r   r   r   Ú<module>.   sN   



)  