
    d                        d dgZ 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ZddlZddl	Z	ddl
mZ ddl
mZmZ ddlmZ dZd	Zd
ZdZ ej                    Zd Zd Z G d de          Z G d d          Zd Z G d de          Z	 	 d%dZd Z G d de          Z  G d d e!          Z" G d de!          Z#e#Z$ G d de#          Z% G d  d!e!          Z& G d" d#e&          Z' G d$ de"          Z(dS )&Pool
ThreadPool    N   )util)get_contextTimeoutError)waitINITRUNCLOSE	TERMINATEc                 .    t          t          |            S N)listmapargss    &lib/python3.11/multiprocessing/pool.pymapstarr   /   s    T
    c                 ^    t          t          j        | d         | d                             S )Nr   r   )r   	itertoolsstarmapr   s    r   starmapstarr   2   s%    	!$q'4733444r   c                       e Zd Zd Zd ZdS )RemoteTracebackc                     || _         d S r   tb)selfr   s     r   __init__zRemoteTraceback.__init__:   s    r   c                     | j         S r   r   r    s    r   __str__zRemoteTraceback.__str__<   s	    wr   N)__name__
__module____qualname__r!   r$    r   r   r   r   9   s2              r   r   c                       e Zd Zd Zd ZdS )ExceptionWithTracebackc                     t          j        t          |          ||          }d                    |          }|| _        d|z  | _        d S )N z

"""
%s""")	tracebackformat_exceptiontypejoinexcr   )r    r1   r   s      r   r!   zExceptionWithTraceback.__init__@   sB    'S		3;;WWR[[ 2%r   c                 ,    t           | j        | j        ffS r   )rebuild_excr1   r   r#   s    r   
__reduce__z!ExceptionWithTraceback.__reduce__E   s    TXtw///r   N)r%   r&   r'   r!   r4   r(   r   r   r*   r*   ?   s2        & & &
0 0 0 0 0r   r*   c                 .    t          |          | _        | S r   )r   	__cause__)r1   r   s     r   r3   r3   H   s    #B''CMJr   c                   .     e Zd ZdZ fdZd Zd Z xZS )MaybeEncodingErrorzVWraps possible unpickleable errors, so they can be
    safely sent through the socket.c                     t          |          | _        t          |          | _        t          t          |                               | j        | j                   d S r   )reprr1   valuesuperr8   r!   )r    r1   r;   	__class__s      r   r!   zMaybeEncodingError.__init__T   sH    99%[[
 $''004:FFFFFr   c                 (    d| j         d| j        dS )NzError sending result: 'z'. Reason: '')r;   r1   r#   s    r   r$   zMaybeEncodingError.__str__Y   s"     =AZZZ=AXXXG 	Gr   c                 (    d| j         j        d| dS )N<z: >)r=   r%   r#   s    r   __repr__zMaybeEncodingError.__repr__]   s     !^444ddd;;r   )r%   r&   r'   __doc__r!   r$   rC   __classcell__r=   s   @r   r8   r8   P   sh        ' 'G G G G G
G G G< < < < < < <r   r8   r(   Fc                    |=t          |t                    r|dk    s"t          d                    |                    |j        }| j        }t          | d          r2| j                                         |j	                                         | ||  d}|
|r%||k     r	  |            }	n+# t          t          f$ r t          j        d           Y nw xY w|	t          j        d           n|	\  }
}}}}	 d ||i |f}n;# t          $ r.}|r|t          urt!          ||j                  }d|f}Y d }~nd }~ww xY w	  ||
||f           nT# t          $ rG}t%          ||d                   }t          j        d	|z              ||
|d|ff           Y d }~nd }~ww xY wd x}	x}
x}x}x}}|dz  }||r||k     t          j        d
|z             d S )Nr   zMaxtasks {!r} is not valid_writerr   z)worker got EOFError or OSError -- exitingzworker got sentinel -- exitingTFz0Possible encoding error while sending result: %szworker exiting after %d tasks)
isinstanceintAssertionErrorformatputgethasattrrH   close_readerEOFErrorOSErrorr   debug	Exception_helper_reraises_exceptionr*   __traceback__r8   )inqueueoutqueueinitializerinitargsmaxtaskswrap_exceptionrM   rN   	completedtaskjobifuncr   kwdsresultewrappeds                     r   workerrg   a   s   z(C'@'@+3q==9@@JJKKK
,C
+Cw	"" !   XI

x
I,@,@	355DD'" 	 	 	JBCCCE	 <J7888#' QdD	 DD$/$//0FF 	  	  	  ?$.H"H"H*1ao>>QZFFFFFF	 	,Ca !!!! 	, 	, 	,(F1I66GJI   Ca%)*++++++++		, 4877s7V7d7TDQ	7 
x
I,@,@8 	J.:;;;;;sB   &
B1 1%CC<
D 
D?$D::D?E 
F#=FF#c                     | )z@Pickle-able helper function for use by _guarded_task_generation.r(   )exs    r   rV   rV      s    
Hr   c                   2     e Zd ZdZdd fd
Z fdZ xZS )
_PoolCachez
    Class that implements a cache for the Pool class that will notify
    the pool management threads every time the cache is emptied. The
    notification is done by the use of a queue that is provided when
    instantiating the cache.
    Nnotifierc               H    || _          t                      j        |i | d S r   )rm   r<   r!   )r    rm   r   rc   r=   s       r   r!   z_PoolCache.__init__   s,     $'$'''''r   c                     t                                          |           | s| j                            d            d S d S r   )r<   __delitem__rm   rM   )r    itemr=   s     r   rp   z_PoolCache.__delitem__   sK    D!!!  	$Md#####	$ 	$r   )r%   r&   r'   rD   r!   rp   rE   rF   s   @r   rk   rk      sj          +/ ( ( ( ( ( ( (
$ 
$ 
$ 
$ 
$ 
$ 
$ 
$ 
$r   rk   c                      e Zd ZdZdZed             Z	 	 d)dZej	        e
fdZd Zd	 Zed
             Zed             Zd Zed             Zed             Zd Zd Zdi fdZd*dZd*dZ	 	 d+dZd Zd,dZd,dZdi ddfdZ	 	 d+dZ	 	 d+dZed*d            Ze d             Z!ed             Z"ed             Z#ed              Z$d! Z%d" Z&d# Z'd$ Z(ed%             Z)e d&             Z*d' Z+d( Z,dS )-r   zS
    Class which supports an async version of applying functions to arguments.
    Tc                      | j         |i |S r   Process)ctxr   rc   s      r   ru   zPool.Process   s    s{D)D)))r   Nr(   c                 &   g | _         t          | _        |pt                      | _        |                                  t          j                    | _        | j                                        | _	        t          | j	                  | _        || _        || _        || _        |t          j                    pd}|dk     rt#          d          |*t%          |t&                    r|dk    rt#          d          |t)          |          st+          d          || _        	 |                                  nR# t0          $ rE | j         D ]}|j        |                                 | j         D ]}|                                  w xY w|                                 }t;          j        t>          j         | j        | j        | j        | j!        | j        | j         | j"        | j#        | j        | j        | j        | j$        || j	        f          | _%        d| j%        _&        tN          | j%        _        | j%        (                                 t;          j        t>          j)        | j        | j*        | j#        | j         | j        f          | _+        d| j+        _&        tN          | j+        _        | j+        (                                 t;          j        t>          j,        | j#        | j-        | j        f          | _.        d| j.        _&        tN          | j.        _        | j.        (                                 t_          j0        | | j1        | j        | j"        | j#        | j         | j	        | j%        | j+        | j.        | j        f	d	
          | _2        tN          | _        d S )Nrl   r   z&Number of processes must be at least 1r   z/maxtasksperchild must be a positive int or Nonezinitializer must be a callabletargetr   T   )r   exitpriority)3_poolr
   _stater   _ctx_setup_queuesqueueSimpleQueue
_taskqueue_change_notifierrk   _cache_maxtasksperchild_initializer	_initargsos	cpu_count
ValueErrorrI   rJ   callable	TypeError
_processes_repopulate_poolrU   exitcode	terminater0   _get_sentinels	threadingThreadr   _handle_workersru   _inqueue	_outqueue_wrap_exception_worker_handlerdaemonr   start_handle_tasks
_quick_put_task_handler_handle_results
_quick_get_result_handlerr   Finalize_terminate_pool
_terminate)r    	processesrZ   r[   maxtasksperchildcontextp	sentinelss           r   r!   zPool.__init__   s_    
,{}}	+-- !%	 5 5 7 7 $*?@@@!1'!+!Iq==EFFF'.44 T8HA8M8M !RSSS"8K+@+@"<===#	!!#### 	 	 	Z " ":%KKMMMZ  	 ''))	(/'+t	4</4:t}dn#T^T5K&	43HJ      '+#&)#""$$$ '-%/4?DN*dk+  
 %)!$'!  """(/'.$/4;?      '+#&)#""$$$-$&/4=$.$*')=t?Q&5    s   "D7 7AFc                     | j         |k    rC |d| t          |            t          | dd           | j                            d            d S d S d S )Nz&unclosed running multiprocessing pool )sourcer   )r}   ResourceWarninggetattrr   rM   )r    _warnr   s      r   __del__zPool.__del__
  sy    ;#EC4CC!$0 0 0 0t/66B%))$/////	  CBr   c           
      p    | j         }d|j         d|j         d| j         dt	          | j                   d	S )NrA   .z state=z pool_size=rB   )r=   r&   r'   r}   lenr|   )r    clss     r   rC   zPool.__repr__  sY    n0CN 0 0S%5 0 00 0 __0 0 0 	1r   c                 B    | j         j        g}| j        j        g}g ||S r   )r   rQ   r   )r    task_queue_sentinelsself_notifier_sentinelss      r   r   zPool._get_sentinels  s1     $ 67#'#8#@"A@%@(?@@r   c                     d | D             S )Nc                 <    g | ]}t          |d           |j        S )sentinel)rO   r   ).0rg   s     r   
<listcomp>z.Pool._get_worker_sentinels.<locals>.<listcomp>  s8     8 8 8F"6:668 8 8 8r   r(   workerss    r   _get_worker_sentinelszPool._get_worker_sentinels  s#    8 88 8 8 	8r   c                     d}t          t          t          |                               D ]A}| |         }|j        0t	          j        d|z             |                                 d}| |= B|S )zCleanup after any worker processes which have exited due to reaching
        their specified lifetime.  Returns True if any workers were cleaned up.
        FNcleaning up worker %dT)reversedranger   r   r   rT   r0   )poolcleanedra   rg   s       r   _join_exited_workerszPool._join_exited_workers!  st    
 %D		**++ 	 	A!WF*
2Q6777Gr   c                     |                      | j        | j        | j        | j        | j        | j        | j        | j        | j	        | j
        
  
        S r   )_repopulate_pool_staticr~   ru   r   r|   r   r   r   r   r   r   r#   s    r   r   zPool._repopulate_pool1  sN    ++DIt|,0O,0J,0ND<M,0N,0,B,0,@B B 	Br   c
                 D   t          |t          |          z
            D ]}
 || t          ||||||	f          }|j                            dd          |_        d|_        |                                 |                    |           t          j	        d           dS )zBring the number of pool processes up to the specified number,
        for use after reaping workers which have exited.
        rx   ru   
PoolWorkerTzadded workerN)
r   r   rg   namereplacer   r   appendr   rT   )rv   ru   r   r   rX   rY   rZ   r[   r   r]   ra   ws               r   r   zPool._repopulate_pool_static:  s     y3t99,-- 
	' 
	'AF%x)&(8,./ / /A
 V^^I|<<AFAHGGIIIKKNNNJ~&&&&
	' 
	'r   c
                     t                               |          r%t                               | |||||||||	
  
         dS dS )zEClean up any exited workers and start replacements for them.
        N)r   r   r   )
rv   ru   r   r   rX   rY   rZ   r[   r   r]   s
             r   _maintain_poolzPool._maintain_poolM  s^     $$T** 	9((gy$)0(K)13C)79 9 9 9 9	9 	9r   c                     | j                                         | _        | j                                         | _        | j        j        j        | _        | j        j        j        | _	        d S r   )
r~   r   r   r   rH   sendr   rQ   recvr   r#   s    r   r   zPool._setup_queuesY  sN    	--//..00-/4.05r   c                 D    | j         t          k    rt          d          d S )NzPool not running)r}   r   r   r#   s    r   _check_runningzPool._check_running_  s&    ;#/000 r   c                 T    |                      |||                                          S )zT
        Equivalent of `func(*args, **kwds)`.
        Pool must be running.
        )apply_asyncrN   )r    rb   r   rc   s       r   applyz
Pool.applyc  s(    
 dD1155777r   c                 `    |                      ||t          |                                          S )zx
        Apply `func` to each element in `iterable`, collecting the results
        in a list that is returned.
        )
_map_asyncr   rN   r    rb   iterable	chunksizes       r   r   zPool.mapj  s(    
 tXw	BBFFHHHr   c                 `    |                      ||t          |                                          S )z
        Like `map()` method but the elements of the `iterable` are expected to
        be iterables as well and will be unpacked as arguments. Hence
        `func` and (a, b) becomes func(a, b).
        )r   r   rN   r   s       r   r   zPool.starmapq  s(     tX{IFFJJLLLr   c                 @    |                      ||t          |||          S )z=
        Asynchronous version of `starmap()` method.
        )r   r   r    rb   r   r   callbackerror_callbacks         r   starmap_asynczPool.starmap_asyncy  s'    
 tX{I'9 9 	9r   c              #      K   	 d}t          |          D ]\  }}||||fi fV  dS # t          $ r}||dz   t          |fi fV  Y d}~dS d}~ww xY w)zProvides a generator of tasks for imap and imap_unordered with
        appropriate handling for iterables which throw exceptions during
        iteration.r   N)	enumeraterU   rV   )r    
result_jobrb   r   ra   xre   s          r   _guarded_task_generationzPool._guarded_task_generation  s      	JA!(++ 6 61!1dQD"555556 6 	J 	J 	Jqs$>bIIIIIIIIII	Js   !' 
AA		Ar   c                    |                                   |dk    rLt          |           }| j                            |                     |j        ||          |j        f           |S |dk     r"t          d                    |                    t          
                    |||          }t          |           }| j                            |                     |j        t          |          |j        f           d |D             S )zP
        Equivalent of `map()` -- can be MUCH slower than `Pool.map()`.
        r   zChunksize must be 1+, not {0:n}c              3   $   K   | ]}|D ]}|V  d S r   r(   r   chunkrq   s      r   	<genexpr>zPool.imap.<locals>.<genexpr>  /      ??U??D???????r   )r   IMapIteratorr   rM   r   _job_set_lengthr   rL   r   
_get_tasksr   r    rb   r   r   rd   task_batchess         r   imapz	Pool.imap  s!    	>>!$''FO11&+tXNN&  
 M1}} 5<<!# #$ $ $  ??49EEL!$''FO11&+292>@ @ &	   @?f????r   c                    |                                   |dk    rLt          |           }| j                            |                     |j        ||          |j        f           |S |dk     r"t          d                    |                    t          
                    |||          }t          |           }| j                            |                     |j        t          |          |j        f           d |D             S )zL
        Like `imap()` method but ordering of results is arbitrary.
        r   zChunksize must be 1+, not {0!r}c              3   $   K   | ]}|D ]}|V  d S r   r(   r   s      r   r   z&Pool.imap_unordered.<locals>.<genexpr>  r   r   )r   IMapUnorderedIteratorr   rM   r   r   r   r   rL   r   r   r   r   s         r   imap_unorderedzPool.imap_unordered  s    	>>*400FO11&+tXNN&  
 M1}} 5<<YGGI I I??49EEL*400FO11&+292>@ @ &	   @?f????r   c                     |                                   t          | ||          }| j                            |j        d|||fgdf           |S )z;
        Asynchronous version of `apply()` method.
        r   N)r   ApplyResultr   rM   r   )r    rb   r   rc   r   r   rd   s          r   r   zPool.apply_async  sW    
 	T8^<<v{AtT4@A4HIIIr   c                 @    |                      ||t          |||          S )z9
        Asynchronous version of `map()` method.
        )r   r   r   s         r   	map_asynczPool.map_async  s'    
 tXw	8  	r   c                    |                                   t          |d          st          |          }|<t          t	          |          t	          | j                  dz            \  }}|r|dz  }t	          |          dk    rd}t                              |||          }t          | |t	          |          ||          }	| j	        
                    |                     |	j        ||          df           |	S )zY
        Helper function to implement map, starmap and their async counterparts.
        __len__N   r   r   r   )r   rO   r   divmodr   r|   r   r   	MapResultr   rM   r   r   )
r    rb   r   mapperr   r   r   extrar   rd   s
             r   r   zPool._map_async  s	   
 	x++ 	&H~~H%c(mmS__q5HIIIu Q	x==AItXyAA4CMM8*8: : :--fk.4.:< < 		
 	
 	
 r   c                     t          | |           |                                s*|                                 |                                (d S d S )N)timeout)r	   emptyrN   )r   change_notifierr  s      r   _wait_for_updateszPool._wait_for_updates  se    Y((((!'')) 	"!!! "'')) 	" 	" 	" 	" 	"r   c                    t          j                    }|j        t          k    s|r|j        t          k    ro|                     |||||||	|
||
  
         g |                     |          |}|                     ||           |j        t          k    ]|r|j        t          k    o|                    d            t          j
        d           d S )Nzworker handler exiting)r   current_threadr}   r   r   r   r   r	  rM   r   rT   )r   cache	taskqueuerv   ru   r   r   rX   rY   rZ   r[   r   r]   r   r  threadcurrent_sentinelss                    r   r   zPool._handle_workers  s    
 )++ ms""u")1K1KsGYg'h/A A A !O#";";D"A"A NI N!!"3_EEE ms""u")1K1K 	d
+,,,,,r   c                 J   t          j                    }t          | j        d           D ]\  }}d }	 |D ]}|j        t
          k    rt          j        d            n	  ||           5# t          $ rG}	|d d         \  }
}	 ||
         	                    |d|	f           n# t          $ r Y nw xY wY d }	~	d }	~	ww xY w|r.t          j        d           |r|d         nd} ||dz              	 d x}x}}
	 d x}x}}
 n# d x}x}}
w xY wt          j        d           	 t          j        d           |                    d            t          j        d	           |D ]} |d            n$# t          $ r t          j        d
           Y nw xY wt          j        d           d S )Nz'task handler found thread._state != RUN   Fzdoing set_length()r   r   ztask handler got sentinelz/task handler sending sentinel to result handlerz(task handler sending sentinel to workersz/task handler got OSError when sending sentinelsztask handler exiting)r   r  iterrN   r}   r   r   rT   rU   _setKeyErrorrM   rS   )r  rM   rY   r   r  r  taskseq
set_lengthr_   re   r`   idxr   s                r   r   zPool._handle_tasks  sB   )++#'	t#<#< 	4 	4GZD,#  D}++
#LMMM!D				$ ! ! !#'8S!!#JOOC%<<<<' ! ! ! D!	! " ,
#7888)-5d1gg2"
37+++ (,++w '+++wt++w++++J2333
	JJHIIILL JABBB  D				 	J 	J 	JJHIIIII	J 	
)*****sl   *C=A('C=(
B92B4 BB4
B,	)B4+B,	,B4/C=4B993C==DAE+ +FFc                 R   t          j                    }	 	  |            }n,# t          t          f$ r t	          j        d           Y d S w xY w|j        t          k    r/|j        t          k    s
J d            t	          j        d           nR|t	          j        d           n;|\  }}}	 ||         	                    ||           n# t          $ r Y nw xY wd x}x}}|r|j        t          k    r	  |            }n,# t          t          f$ r t	          j        d           Y d S w xY w|t	          j        d           a|\  }}}	 ||         	                    ||           n# t          $ r Y nw xY wd x}x}}|r|j        t          k    t          | d          rdt	          j        d           	 t          d	          D ]'}| j                                        s n |             (n# t          t          f$ r Y nw xY wt	          j        d
t          |          |j                   d S )Nr   z.result handler got EOFError/OSError -- exitingzThread not in TERMINATEz,result handler found thread._state=TERMINATEzresult handler got sentinelz&result handler ignoring extra sentinelrQ   z"ensuring that outqueue is not full
   z7result handler exiting: len(cache)=%s, thread._state=%s)r   r  rS   rR   r   rT   r}   r   r   r  r  rO   r   rQ   pollr   )rY   rN   r  r  r_   r`   ra   objs           r   r   zPool._handle_results=  s   )++	$suuX&   
KLLL }##}	1113L111
IJJJ|
8999KCCc
3''''   ##D#3+	$.  	$22suuX&   
KLLL |
CDDDKCCc
3''''   ##D#3  	$22" 8Y'' 	J;<<<r  A#+0022 CEEEEX&    	
L%jj&-	) 	) 	) 	) 	)sW   
! %A
	A
*C 
CC1
C< <%D%$D%E# #
E0/E007G( (G<;G<c              #      K   t          |          }	 t          t          j        ||                    }|sd S | |fV  -r   )r  tupler   islice)rb   itsizer   s       r   r   zPool._get_tasksy  sO      "XX	i&r40011A )OOO		r   c                      t          d          )Nz:pool objects cannot be passed between processes or pickled)NotImplementedErrorr#   s    r   r4   zPool.__reduce__  s    !J  	r   c                     t          j        d           | j        t          k    r9t          | _        t          | j        _        | j                            d            d S d S )Nzclosing pool)r   rT   r}   r   r   r   r   rM   r#   s    r   rP   z
Pool.close  sV    
>""";#DK*/D '!%%d+++++ r   c                 n    t          j        d           t          | _        |                                  d S )Nzterminating pool)r   rT   r   r}   r   r#   s    r   r   zPool.terminate  s0    
%&&&r   c                    t          j        d           | j        t          k    rt	          d          | j        t
          t          fvrt	          d          | j                                         | j	                                         | j
                                         | j        D ]}|                                 d S )Nzjoining poolzPool is still runningzIn unknown state)r   rT   r}   r   r   r   r   r   r0   r   r   r|   )r    r   s     r   r0   z	Pool.join  s    
>""";#4555[	 222/000!!###!!!!!### 	 	AFFHHHH	 	r   c                 z   t          j        d           | j                                         |                                ru| j                                        r`| j                                         t          j	        d           |                                r| j                                        Zd S d S d S d S )Nz7removing tasks from inqueue until task handler finishedr   )
r   rT   _rlockacquireis_aliverQ   r  r   timesleep)rX   task_handlerr   s      r   _help_stuff_finishzPool._help_stuff_finish  s     	
LMMM   ##%% 	'/*>*>*@*@ 	O  """JqMMM ##%% 	'/*>*>*@*@ 	 	 	 	 	 	 	 	 	r   c
                    t          j        d           t          |_        |                    d            t          |_        t          j        d           |                     ||t          |                     |                                s"t          |	          dk    rt          d          t          |_        |                    d            |                    d            t          j        d           t          j
                    |ur|                                 |rJt          |d         d          r4t          j        d           |D ]}
|
j        |
                                 t          j        d           t          j
                    |ur|                                 t          j        d	           t          j
                    |ur|                                 |rst          |d         d          r_t          j        d
           |D ]J}
|
                                r0t          j        d|
j        z             |
                                 Gd S d S d S )Nzfinalizing poolz&helping task handler/workers to finishr   z.Cannot have cache with result_hander not alivezjoining worker handlerr   zterminating workerszjoining task handlerzjoining result handlerzjoining pool workersr   )r   rT   r   r}   rM   r-  r   r)  rK   r   r  r0   rO   r   r   pid)r   r  rX   rY   r   r  worker_handlerr,  result_handlerr  r   s              r   r   zPool._terminate_pool  sb    	
$%%%
 !*D!!!'
;<<<wc$ii@@@'')) 	BE

a @B B B !*D!!!T 	
+,,,#%%^;;!!!  	"GDG[11 	"J,--- " ":%KKMMM
)***#%%\99
+,,,#%%^;;!!! 	GDG[11 	J-...  ::<< J6>???FFHHH	 	 	 	 r   c                 .    |                                   | S r   )r   r#   s    r   	__enter__zPool.__enter__  s    r   c                 .    |                                   d S r   )r   )r    exc_typeexc_valexc_tbs       r   __exit__zPool.__exit__  s    r   )NNr(   NNr   )NNN)r   )-r%   r&   r'   rD   r   staticmethodru   r!   warningswarnr   r   rC   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r	  classmethodr   r   r   r   r4   rP   r   r0   r-  r   r3  r8  r(   r   r   r   r      sa         O* * \* CE04O O O Of %Ms 0 0 0 01 1 1A A A
 8 8 \8   \B B B ' ' \'$ 	9 	9 \	96 6 61 1 1  " 8 8 8 8I I I IM M M M FJ9 9 9 9	J 	J 	J@ @ @ @:@ @ @ @8 &(b4    BF    KO   : " " " \"
 - - [-( ++ ++ \++Z 9) 9) \9)v   \  
, , ,  

 
 
   \ 3 3 [3j      r   c                   V    e Zd Zd Zd Zd ZddZddZd Z e	e
j                  ZdS )	r   c                     || _         t          j                    | _        t	          t
                    | _        |j        | _        || _        || _	        | | j        | j        <   d S r   )
r|   r   Event_eventnextjob_counterr   r   	_callback_error_callback)r    r   r   r   s       r   r!   zApplyResult.__init__  sS    
o''%%	k!-!%DIr   c                 4    | j                                         S r   )r@  is_setr#   s    r   readyzApplyResult.ready  s    {!!###r   c                 |    |                                  s"t          d                    |                     | j        S )Nz{0!r} not ready)rG  r   rL   _successr#   s    r   
successfulzApplyResult.successful  s7    zz|| 	=.55d;;<<<}r   Nc                 :    | j                             |           d S r   )r@  r	   r    r  s     r   r	   zApplyResult.wait  s    !!!!!r   c                     |                      |           |                                 st          | j        r| j        S | j        r   )r	   rG  r   rI  _valuerL  s     r   rN   zApplyResult.get  sD    		'zz|| 	= 	;+r   c                    |\  | _         | _        | j        r!| j         r|                     | j                   | j        r!| j         s|                     | j                   | j                                         | j        | j        = d | _        d S r   )	rI  rN  rC  rD  r@  setr   r   r|   r    ra   r  s      r   r  zApplyResult._set  s    %("t{> 	(dm 	(NN4;''' 	. 	.  ---K	"


r   r   )r%   r&   r'   r!   rG  rJ  r	   rN   r  r<  typesGenericAlias__class_getitem__r(   r   r   r   r     s        & & &$ $ $  
" " " "      $E$677r   r   c                       e Zd Zd Zd ZdS )r  c                    t                               | |||           d| _        d g|z  | _        || _        |dk    r/d| _        | j                                         | j        | j	        = d S ||z  t          ||z            z   | _        d S )Nr   Tr   )r   r!   rI  rN  
_chunksize_number_leftr@  rP  r   r   bool)r    r   r   lengthr   r   s         r   r!   zMapResult.__init__  s    T4,: 	 	< 	< 	<fvo#>> !DKOODI&&& &	 1D)9K4L4L LDr   c                    | xj         dz  c_         |\  }}|r| j        r||| j        || j        z  |dz   | j        z  <   | j         dk    rP| j        r|                     | j                   | j        | j        = | j                                         d | _	        d S d S |s| j        rd| _        || _        | j         dk    rP| j
        r| 
                    | j                   | j        | j        = | j                                         d | _	        d S d S )Nr   r   F)rX  rI  rN  rW  rC  r   r   r@  rP  r|   rD  )r    ra   success_resultsuccessrd   s        r   r  zMapResult._set)  s1   Q( 	"t} 	"CIDK$/)1Q3*??@ A%%> 0NN4;///K	*!!!!


 &%  %t} % %$ A%%' 6((555K	*!!!!


 &%r   N)r%   r&   r'   r!   r  r(   r   r   r  r    s5        M M M" " " " "r   r  c                   2    e Zd Zd Zd ZddZeZd Zd ZdS )r   c                 *   || _         t          j        t          j                              | _        t          t                    | _        |j        | _        t          j
                    | _        d| _        d | _        i | _        | | j        | j        <   d S )Nr   )r|   r   	ConditionLock_condrA  rB  r   r   collectionsdeque_items_index_length	_unsorted)r    r   s     r   r!   zIMapIterator.__init__G  st    
()9)9::
%%	k!'))!%DIr   c                     | S r   r(   r#   s    r   __iter__zIMapIterator.__iter__R  s    r   Nc                    | j         5  	 | j                                        }n# t          $ r | j        | j        k    rd | _        t          d | j                             |           	 | j                                        }n5# t          $ r( | j        | j        k    rd | _        t          d t          d w xY wY nw xY wd d d            n# 1 swxY w Y   |\  }}|r|S |r   )
rb  re  popleft
IndexErrorrf  rg  r|   StopIterationr	   r   )r    r  rq   r]  r;   s        r   rA  zIMapIterator.nextU  sE   Z 	1 	11{**,, 1 1 1;$,..!%DJ'T1
(((1;..00DD! 1 1 1{dl22%)
+5&D0	1 D1	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1   	LsJ   C	$C	AB:(BB:2B44B:7C	9B::C		CCc                 
   | j         5  | j        |k    r| j                            |           | xj        dz  c_        | j        | j        v rW| j                            | j                  }| j                            |           | xj        dz  c_        | j        | j        v W| j                                          n
|| j        |<   | j        | j        k    r| j        | j	        = d | _
        d d d            d S # 1 swxY w Y   d S Nr   )rb  rf  re  r   rh  popnotifyrg  r   r   r|   rQ  s      r   r  zIMapIterator._setm  sE   Z 	" 	"{a""3'''q kT^33.,,T[99CK&&s+++KK1$KK kT^33 
!!####$'q!{dl**K	*!
	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	"s   C#C88C<?C<c                     | j         5  || _        | j        | j        k    r-| j                                          | j        | j        = d | _        d d d            d S # 1 swxY w Y   d S r   )rb  rg  rf  rr  r   r   r|   )r    rZ  s     r   r   zIMapIterator._set_length~  s    Z 	" 	"!DL{dl**
!!###K	*!
	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	"s   AAA!Ar   )	r%   r&   r'   r!   rj  rA  __next__r  r   r(   r   r   r   r   E  sk        	& 	& 	&     , H" " """ " " " "r   r   c                       e Zd Zd ZdS )r   c                    | j         5  | j                            |           | xj        dz  c_        | j                                          | j        | j        k    r| j        | j        = d | _        d d d            d S # 1 swxY w Y   d S rp  )	rb  re  r   rf  rr  rg  r   r   r|   rQ  s      r   r  zIMapUnorderedIterator._set  s    Z 	" 	"Ks###KK1KKJ{dl**K	*!
	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	"s   A(A==BBN)r%   r&   r'   r  r(   r   r   r   r     s#        " " " " "r   r   c                   n    e Zd ZdZed             ZddZd Zd Zed             Z	ed	             Z
d
 ZdS )r   Fc                     ddl m}  ||i |S )Nr   rt   )dummyru   )rv   r   rc   ru   s       r   ru   zThreadPool.Process  s)    """"""w%%%%r   Nr(   c                 @    t                               | |||           d S r   )r   r!   )r    r   rZ   r[   s       r   r!   zThreadPool.__init__  s     dI{H=====r   c                     t          j                    | _        t          j                    | _        | j        j        | _        | j        j        | _        d S r   )r   r   r   r   rM   r   rN   r   r#   s    r   r   zThreadPool._setup_queues  s>    )++*,,-+.,r   c                     | j         j        gS r   )r   rQ   r#   s    r   r   zThreadPool._get_sentinels  s    %-..r   c                     g S r   r(   r   s    r   r   z ThreadPool._get_worker_sentinels  s    	r   c                     	 	 |                      d           # t          j        $ r Y nw xY wt          |          D ]}|                     d            d S )NTF)block)rN   r   Emptyr   rM   )rX   r,  r   ra   s       r   r-  zThreadPool._help_stuff_finish  s{    	)%(((){ 	 	 	D	t 	 	AKK	 	s    ,,c                 .    t          j        |           d S r   )r*  r+  )r    r   r  r  s       r   r	  zThreadPool._wait_for_updates  s    
7r   )NNr(   )r%   r&   r'   r   r9  ru   r!   r   r   r   r-  r	  r(   r   r   r   r     s        O& & \&> > > >- - -/ / /   \   \    r   )Nr(   NF))__all__rc  r   r   r   r   r*  r-   rR  r:  r,   r   r   r   
connectionr	   r
   r   r   r   countrB  r   r   rU   r   r*   r3   r8   rg   rV   dictrk   objectr   r   AsyncResultr  r   r   r   r(   r   r   <module>r     s   <
          				                   ' ' ' ' ' ' ' '       	 io  5 5 5    i   0 0 0 0 0 0 0 0  < < < < < < < <" GK+< +< +< +<Z  $ $ $ $ $ $ $ $.v v v v v6 v v vx)8 )8 )8 )8 )8& )8 )8 )8V %" %" %" %" %" %" %" %"V?" ?" ?" ?" ?"6 ?" ?" ?"J	" 	" 	" 	" 	"L 	" 	" 	"$ $ $ $ $ $ $ $ $ $r   