
    ;e                     8   	 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mZ  ej                    Zda G d d	          Zd
 Z e
j        e           dZdZ G d de          Z G d d          Zd Z G d de          Z G d de          Z  G d de          Z! G d de	          Z"d Z#d Z$	 	 d&dZ%d'dZ& G d de
j'                  Z(da)da*d  Z+d! Z, G d" d#ej-                  Z. G d$ d%ej/                  Z0dS )(z"Brian Quinlan (brian@sweetapp.com)    N)_base)Queue)partial)format_exceptionFc                   &    e Zd Zd Zd Zd Zd ZdS )_ThreadWakeupc                 X    d| _         t          j        d          \  | _        | _        d S )NF)duplex)_closedmpPipe_reader_writerselfs    1/usr/lib/python3.11/concurrent/futures/process.py__init__z_ThreadWakeup.__init__C   s(    %'WE%:%:%:"dlll    c                     | j         s;d| _         | j                                         | j                                         d S d S NT)r   r   closer   r   s    r   r   z_ThreadWakeup.closeG   sK    | 	!DLL   L     	! 	!r   c                 L    | j         s| j                            d           d S d S )Nr   )r   r   
send_bytesr   s    r   wakeupz_ThreadWakeup.wakeupM   s2    | 	)L##C(((((	) 	)r   c                     | j         sM| j                                        r6| j                                         | j                                        2d S d S d S N)r   r   poll
recv_bytesr   s    r   clearz_ThreadWakeup.clearQ   sl    | 	*,##%% *''))) ,##%% * * *	* 	** *r   N)__name__
__module____qualname__r   r   r   r    r   r   r   r   B   sP        ; ; ;! ! !) ) )* * * * *r   r   c                      da t          t                                                    } | D ]\  }}|                                 | D ]\  }}|                                 d S r   )_global_shutdownlist_threads_wakeupsitemsr   join)r(   _thread_wakeupts       r   _python_exitr-   W   sw    !''))**E!  =  1	 r      =   c                       e Zd Zd Zd ZdS )_RemoteTracebackc                     || _         d S r   tb)r   r4   s     r   r   z_RemoteTraceback.__init__w   s    r   c                     | j         S r   r3   r   s    r   __str__z_RemoteTraceback.__str__y   s	    wr   N)r    r!   r"   r   r6   r#   r   r   r1   r1   v   s2              r   r1   c                       e Zd Zd Zd ZdS )_ExceptionWithTracebackc                     d                     t          t          |          ||                    }|| _        d | j        _        d|z  | _        d S )N z

"""
%s""")r)   r   typeexc__traceback__r4   )r   r<   r4   s      r   r   z _ExceptionWithTraceback.__init__}   sI    WW%d3iib99:: "& 2%r   c                 ,    t           | j        | j        ffS r   )_rebuild_excr<   r4   r   s    r   
__reduce__z"_ExceptionWithTraceback.__reduce__   s    dh000r   N)r    r!   r"   r   r@   r#   r   r   r8   r8   |   s2        & & &1 1 1 1 1r   r8   c                 .    t          |          | _        | S r   )r1   	__cause__)r<   r4   s     r   r?   r?      s    $R((CMJr   c                       e Zd Zd ZdS )	_WorkItemc                 >    || _         || _        || _        || _        d S r   )futurefnargskwargs)r   rF   rG   rH   rI   s        r   r   z_WorkItem.__init__   s"    	r   Nr    r!   r"   r   r#   r   r   rD   rD      #            r   rD   c                       e Zd ZddZdS )_ResultItemNc                 >    || _         || _        || _        || _        d S r   )work_id	exceptionresultexit_pid)r   rO   rP   rQ   rR   s        r   r   z_ResultItem.__init__   s"    " r   NNNrJ   r#   r   r   rM   rM      s(        ! ! ! ! ! !r   rM   c                       e Zd Zd ZdS )	_CallItemc                 >    || _         || _        || _        || _        d S r   )rO   rG   rH   rI   )r   rO   rG   rH   rI   s        r   r   z_CallItem.__init__   s"    	r   NrJ   r#   r   r   rU   rU      rK   r   rU   c                   ,     e Zd Z	 d fd	Z fdZ xZS )
_SafeQueuer   c                x    || _         || _        || _        t                                          ||           d S )N)ctx)pending_work_itemsshutdown_lockr+   superr   )r   max_sizerZ   r[   r\   r+   	__class__s         r   r   z_SafeQueue.__init__   s>    "4**s+++++r   c                     t          |t                    rt          t          |          ||j                  }t          d                    d                    |                              |_        | j	        
                    |j        d           }| j        5  | j                                         d d d            n# 1 swxY w Y   ||j                            |           d S d S t#                                          ||           d S )Nz

"""
{}"""r:   )
isinstancerU   r   r;   r=   r1   formatr)   rB   r[   poprO   r\   r+   r   rF   set_exceptionr]   _on_queue_feeder_error)r   eobjr4   	work_itemr_   s        r   re   z!_SafeQueue._on_queue_feeder_error   s1   c9%% 	3!$q''1ao>>B*>+@+@+M+MNNAK/33CKFFI# , ,"))+++, , , , , , , , , , , , , , ,
 $ ..q11111 %$ GG**1c22222s   CCC)r   )r    r!   r"   r   re   __classcell__r_   s   @r   rX   rX      sY        G, , , , , ,3 3 3 3 3 3 3 3 3r   rX   c              '   r   K   	 t          | }	 t          t          j        ||                     }|sd S |V  +r   )ziptuple	itertoolsislice)	chunksize	iterablesitchunks       r   _get_chunksrt      sL      6	iBi&r95566 	F	r   c                 "     	  fd|D             S )Nc                     g | ]} | S r#   r#   ).0rH   rG   s     r   
<listcomp>z"_process_chunk.<locals>.<listcomp>   s    ((($BBI(((r   r#   )rG   rs   s   ` r   _process_chunkry      s$     )(((%((((r   c                     	 	 |                      t          ||||                     d S # t          $ rE}t          ||j                  }|                      t          |||                     Y d }~d S d }~ww xY w)N)rQ   rP   rR   rP   rR   )putrM   BaseExceptionr8   r=   )result_queuerO   rQ   rP   rR   rf   r<   s          r   _sendback_resultr      s    89WV/88M M M 	N 	N 	N 	N 	N 9 9 9%a99W.68 8 8 	9 	9 	9 	9 	9 	9 	9 	9 	99s   &+ 
A::A55A:c                 <   	 |9	  ||  n2# t           $ r% t          j                            dd           Y d S w xY wd}d }	 |                     d          }|(|                    t          j                               d S ||dz  }||k    rt          j                    }	  |j        |j	        i |j
        }t          ||j        ||           ~nD# t           $ r7}	t          |	|	j                  }
t          ||j        |
|           Y d }	~	nd }	~	ww xY w~|d S )	NzException in initializer:T)exc_infor   blockr.   )rQ   rR   r{   )r}   r   LOGGERcriticalgetr|   osgetpidrG   rH   rI   r   rO   r8   r=   )
call_queuer~   initializerinitargs	max_tasks	num_tasksrR   	call_itemrrf   r<   s              r   _process_workerr      s    	K""" 	 	 	L!!"=!MMM FF		
 IHNNN..	RY[[)))F NII%%9;;			inA	0@AAA \9+<Q&.0 0 0 0  	0 	0 	0)!Q_==C\9+<&.0 0 0 0 0 0 0 0 0	0 F9s$    +::#C 
D-DDc                   \     e Zd Z	  fdZd Zd Zd Zd Zd Zd Z	d Z
d	 Zd
 Zd Z xZS )_ExecutorManagerThreadc                 \   |j         | _        |j        | _        | j        | j        fd}t	          j        ||          | _        |j        | _        |j	        | _
        |j        | _        |j        | _        |j        | _        |j        | _        t'                                                       d S )Nc                     t           j                            d           |5  |                                 d d d            d S # 1 swxY w Y   d S )Nz?Executor collected: triggering callback for QueueManager wakeup)r   utildebugr   )r*   r+   r\   s      r   
weakref_cbz3_ExecutorManagerThread.__init__.<locals>.weakref_cb,  s     GMM 1 2 2 2 ' '$$&&&' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' 's   AAA)_executor_manager_thread_wakeupr+   _shutdown_lockr\   weakrefrefexecutor_reference
_processes	processes_call_queuer   _result_queuer~   	_work_idswork_ids_queue_max_tasks_per_childmax_tasks_per_child_pending_work_itemsr[   r]   r   )r   executorr   r_   s      r   r   z_ExecutorManagerThread.__init__  s    
 &E%4 &*%7%)%7	' 	' 	' 	' #*+h
"C"C ", #. %2 '0 $,#@  #+">r   c                    	 |                                   |                                 \  }}}|r|                     |           d S ||                     |           |j        d u}|r3| j                            |j                  }|                                 ~|                                 x}rP|r4| j	        5  |
                                 d d d            n# 1 swxY w Y   n|j                                         ~|                                 rE|                                  |                                   | j        s|                                  d S \r   )add_call_item_to_queuewait_result_broken_or_wakeupterminate_brokenprocess_result_itemrR   r   rc   r)   r   r\   _adjust_process_count_idle_worker_semaphorereleaseis_shutting_downflag_executor_shutting_downr[   join_executor_internals)r   result_item	is_brokencauseprocess_exitedpr   s          r   runz_ExecutorManagerThread.runM  s   (	''))),0,M,M,O,O)KE %%e,,,&((555!,!5T!A! **;+?@@AFFHHH  #668888 !% B!/ = =$::<<<= = = = = = = = = = = = = = = !7??AAA $$&& 00222
 ++--- . 00222FQ(	s   <CC!$C!c                 v   	 | j                                         rd S 	 | j                            d          }| j        |         }|j                                        r<| j                             t          ||j	        |j
        |j                  d           n| j        |= # t          j        $ r Y d S w xY w)NTFr   )r   fullr   r   r[   rF   set_running_or_notify_cancelr|   rU   rG   rH   rI   queueEmpty)r   rO   rh   s      r   r   z-_ExecutorManagerThread.add_call_item_to_queuez  s    	##%% -111>> !3G<	#@@BB O''	'2;,2;.2;2B)D )D /3	 ( 4 4 4 4 /8 ;   	s   B$ $B76B7c                    | j         j        }| j        j        }||g}d t          | j                                                  D             }t          j                            ||z             }d }d}d }||v rR	 |	                                }d}n@# t          $ r-}	t          t          |	          |	|	j                  }Y d }	~	nd }	~	ww xY w||v rd}| j        5  | j                                         d d d            n# 1 swxY w Y   |||fS )Nc                     g | ]	}|j         
S r#   )sentinelrw   r   s     r   rx   zG_ExecutorManagerThread.wait_result_broken_or_wakeup.<locals>.<listcomp>  s    NNN1AJNNNr   TF)r~   r   r+   r&   r   valuesr   
connectionwaitrecvr}   r   r;   r=   r\   r   )
r   result_readerwakeup_readerreadersworker_sentinelsreadyr   r   r   rf   s
             r   r   z3_ExecutorManagerThread.wait_result_broken_or_wakeup  s{    )1*2 -0NNT^5J5J5L5L0M0MNNN""7-=#=>>	E!!F+0022!		  F F F(a!Q_EEF e##I 	' 	'$$&&&	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' Iu,,s*   :B 
C#CCC>>DDc                    t          |t                    rM| j                            |          }|                                 | j        s|                                  d S d S | j                            |j        d           }|I|j        r!|j	        
                    |j                   d S |j	                            |j                   d S d S r   )ra   intr   rc   r)   r   r[   rO   rP   rF   rd   
set_resultrQ   )r   r   r   rh   s       r   r   z*_ExecutorManagerThread.process_result_item  s     k3'' 	D "";//AFFHHH> ,,... 
 /33K4GNNI$( D$22;3HIIIII$//0BCCCCC	 %$r   c                 N    |                                  }t          p
|d u p|j        S r   )r   r%   _shutdown_thread)r   r   s     r   r   z'_ExecutorManagerThread.is_shutting_down  s4    **,,
 ! -H$4 -,	.r   c                 4   |                                  }|d|_        d|_        d }t          d          }|+t	          dd                    |           d          |_        | j                                        D ] \  }}|j	        
                    |           ~!| j                                         | j                                        D ]}|                                 | j        j                                         |                                  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''')r   _brokenr   BrokenProcessPoolr1   r)   rB   r[   r(   rF   rd   r   r   r   	terminater   r   r   r   )r   r   r   bperO   rh   r   s          r   r   z'_ExecutorManagerThread.terminate_broken  s2    **,,!1H )-H%H   !6 7 7 ,-"''%..---/ /CM #'"9"?"?"A"A 	 	GY**3///	%%''' &&(( 	 	AKKMMMM 	%%''' 	$$&&&&&r   c                 T   |                                  }|d|_        |j        ri }| j                                        D ]#\  }}|j                                        s|||<   $|| _        	 	 | j                                         n# t          j
        $ r Y nw xY w1d|_        d S d S d S )NTF)r   r   _cancel_pending_futuresr[   r(   rF   cancelr   
get_nowaitr   r   )r   r   new_pending_work_itemsrO   rh   s        r   r   z2_ExecutorManagerThread.flag_executor_shutting_down  s     **,,(,H%/ 9 *,&*.*A*G*G*I*I D D&GY$+2244 D:C.w7*@'+668888 ;    49000)  9 9s   -B BBc                 L   |                                  }d}||k     r|                                  dk    rmt          ||z
            D ]8}	 | j                            d            |dz  }## t          j        $ r Y  nw xY w||k     r|                                  dk    id S d S d S d S )Nr   r.   )get_n_children_aliveranger   
put_nowaitr   Full)r   n_children_to_stopn_sentinels_sentis       r   shutdown_workersz'_ExecutorManagerThread.shutdown_workers  s    !6688  "444--//!33-0@@AA  O..t444$)$$z   EE  "444--//!3333 5433 54s   
A**A=<A=c                 b   |                                   | j                                         | j                                         | j        5  | j                                         d d d            n# 1 swxY w Y   | j                                        D ]}|                                 d S r   )	r   r   r   join_threadr\   r+   r   r   r)   r   r   s     r   r   z._ExecutorManagerThread.join_executor_internals#  s    ##%%% 	' 	'$$&&&	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' &&(( 	 	AFFHHHH	 	s   A44A8;A8c                 b    t          d | j                                        D                       S )Nc              3   >   K   | ]}|                                 V  d S r   )is_aliver   s     r   	<genexpr>z>_ExecutorManagerThread.get_n_children_alive.<locals>.<genexpr>1  s*      AAA1::<<AAAAAAr   )sumr   r   r   s    r   r   z+_ExecutorManagerThread.get_n_children_alive/  s-    AA)>)>)@)@AAAAAAr   )r    r!   r"   r   r   r   r   r   r   r   r   r   r   r   ri   rj   s   @r   r   r     s        	- - - - -^+ + +Z  .- - ->D D D.. . .(' (' ('T9 9 94  
 
 
B B B B B B Br   r   c                  L   t           rt          rt          t                    da 	 dd l} n$# t          $ r dat          t                    w xY w	 t          j        d          }n# t          t          f$ r Y d S w xY w|dk    rd S |dk    rd S d|z  at          t                    )NTr   zxThis Python build lacks multiprocessing.synchronize, usually due to named semaphores being unavailable on this platform.SC_SEM_NSEMS_MAX   z@system provides too few semaphores (%d available, 256 necessary))	_system_limits_checked_system_limitedNotImplementedErrormultiprocessing.synchronizeImportErrorr   sysconfAttributeError
ValueError)multiprocessing	nsems_maxs     r   _check_system_limitsr   8  s     7 	7%o666!3***** 3 3 3F 	 "/2223J122		J'    B 	C 	46?@O
o
.
..s   + !AA% %A:9A:c              #   r   K   	 | D ]0}|                                  |r|                                V  |1d S r   )reverserc   )iterableelements     r   _chain_from_iterable_of_listsr   X  s]      
      	 ++--  	    r   c                       e Zd ZdS )r   N)r    r!   r"   r#   r   r   r   r   d  s         r   r   c                        e Zd Z	 	 ddddZd Zd Zd Zd Zd	 Ze	j
        j        j        e_        dd
d fd
ZddddZe	j
        j        j        e_         xZS )ProcessPoolExecutorNr#   )r   c                   	 t                       |Jt          j                    pd| _        t          j        dk    rt          t          | j                  | _        nN|dk    rt          d          t          j        dk    r"|t          k    rt          dt                     || _        |*|t          j
        d          }nt          j
                    }|| _        | j                            d          d	k    | _        |t          |          st          d
          || _        || _        |gt%          |t&                    st          d          |dk    rt          d          | j                            d          d	k    rt          d          || _        d | _        i | _        d| _        t1          j                    | _        t1          j        d          | _        d| _        d| _        i | _        d| _         tC                      | _"        | j        tF          z   }tI          || j        | j        | j        | j"                  | _%        d| j%        _&        |'                                | _(        tS          j*                    | _+        d S )Nr.   win32r   z"max_workers must be greater than 0zmax_workers must be <= spawnF)
allow_noneforkzinitializer must be a callablez&max_tasks_per_child must be an integerz max_tasks_per_child must be >= 1zpmax_tasks_per_child is incompatible with the 'fork' multiprocessing start method; supply a different mp_context.)r^   rZ   r[   r\   r+   T),r   r   	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	_initargsra   r   r   _executor_manager_threadr   r   	threadingLockr   	Semaphorer   r   _queue_countr   r   r   r   EXTRA_QUEUED_CALLSrX   r   _ignore_epipeSimpleQueuer   r   r   r   )r   max_workers
mp_contextr   r   r   
queue_sizes          r   r   zProcessPoolExecutor.__init__l  s   	$ 	 " 3!D|w&&$'(<(,(9%; %;! a !EFFF,'))222 D.BDDF F F !,D".^G44

^--
%  11U1CCvM 	0 "8K+@+@"<==='!*1377 E HIII$)) !CDDD00E0BBfLL  "C D D D %8! )-%  !&'n..&/&9!&<&<##% ',$ 0=, &);;
%T%5#7->	@ @ @ *.&'3355r   c                     | j         ^| j        s|                                  t          |           | _         | j                                          | j        t          | j         <   d S d S r   )r  r  _launch_processesr   startr   r'   r   s    r   _start_executor_manager_threadz2ProcessPoolExecutor._start_executor_manager_thread  sn    (0; )&&(((,B4,H,HD))//1114 T:;;; 10r   c                     | j                             d          rd S t          | j                  }|| j        k     r|                                  d S d S )NF)blocking)r   acquirelenr   r  _spawn_process)r   process_counts     r   r   z)ProcessPoolExecutor._adjust_process_count  sb    &...>> 	FDO,,4,,, !!!!! -,r   c                     t          t          | j                  | j                  D ]}|                                  d S r   )r   r&  r   r  r'  )r   r*   s     r   r   z%ProcessPoolExecutor._launch_processes  sI    
 s4?++T->?? 	" 	"A!!!!	" 	"r   c                     | j                             t          | j        | j        | j        | j        | j        f          }|                                 || j	        |j
        <   d S )N)targetrH   )r  Processr   r   r   r  r  r   r!  r   pidr   s     r   r'  z"ProcessPoolExecutor._spawn_process  sg    $$""$#.+	- % . . 	
			!"r   c                ,   | j         5  | j        rt          | j                  | j        rt	          d          t
          rt	          d          t          j                    }t          ||||          }|| j	        | j
        <   | j                            | j
                   | xj
        dz  c_
        | j                                         | j        r|                                  |                                  |cd d d            S # 1 swxY w Y   d S )Nz*cannot schedule new futures after shutdownz6cannot schedule new futures after interpreter shutdownr.   )r   r   r   r   RuntimeErrorr%   r   FuturerD   r   r  r   r|   r   r   r  r   r"  )r   rG   rH   rI   fws         r   submitzProcessPoolExecutor.submit  sg     	 	| 6'555$ Q"#OPPP ;" $: ; ; ; A!Rv..A:;D$T%67Nt0111"0779997 -**,,,//111+	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	s   C4D		DDr.   )timeoutrp   c                    	 |dk     rt          d          t                                          t          t          |          t          |d|i|          }t          |          S )Nr.   zchunksize must be >= 1.rp   )r4  )r   r]   mapr   ry   rt   r   )r   rG   r4  rp   rq   resultsr_   s         r   r6  zProcessPoolExecutor.map  sm    	( q==6777''++gnb99)9J	JJ&-  / / -W555r   TF)cancel_futuresc                n   | j         5  || _        d| _        | j        | j                                         d d d            n# 1 swxY w Y   | j        |r| j                                         d | _        d | _        | j        |r| j        	                                 d | _        d | _
        d | _        d S r   )r   r   r   r   r   r  r)   r   r   r   r   )r   r   r8  s      r   shutdownzProcessPoolExecutor.shutdown9  s     	> 	>+9D($(D!3?4;;===	> 	> 	> 	> 	> 	> 	> 	> 	> 	> 	> 	> 	> 	> 	> (44)..000 )-%)d)$$&&&!/3,,,s   /AA
A)NNNr#   )T)r    r!   r"   r   r"  r   r   r'  r3  r   Executor__doc__r6  r:  ri   rj   s   @r   r   r   k  s        48,.i'GKi' i' i' i' i'V5 5 5" " "" " "	# 	# 	#  . ^*2FN*.! 6 6 6 6 6 6 6:4E 4 4 4 4 4( ~.6Hr   r   rS   r   )1
__author__r   concurrent.futuresr   r   r   r   multiprocessing.connectionmultiprocessing.queuesr   r  r   	functoolsr   rn   r  	tracebackr   WeakKeyDictionaryr'   r%   r   r-   _register_atexitr  r  	Exceptionr1   r8   r?   objectrD   rM   rU   rX   rt   ry   r   r   Threadr   r   r   r   r   BrokenExecutorr   r;  r   r#   r   r   <module>rI     sT  (T 2
 				 $ $ $ $ $ $      ! ! ! ! ( ( ( ( ( (                



 & & & & & & -7,..  * * * * * * * **   	 < ( ( (        y   	1 	1 	1 	1 	1 	1 	1 	1         ! ! ! ! !& ! ! !       3 3 3 3 3 3 3 32  	) 	) 	) DH"	9 	9 	9 	93 3 3 3l_B _B _B _B _BY- _B _B _BD	  / / /@	  	  	     ,   b7 b7 b7 b7 b7%. b7 b7 b7 b7 b7r   