
    F\h                         S S/r SSKrSSKrSSKrSSKrSSKrSSKrSSKrSSKrSSK	r	SSK
Jr  SSK
JrJr  SSKJr  SrS	rS
rSr\R(                  " 5       rS rS r " S S\5      r " S S5      rS r " S S\5      r  S#S jrS r " S S\5      r  " S S \!5      r" " S S\!5      r#\#r$ " S S\#5      r% " S S\!5      r& " S  S!\&5      r' " S" S\"5      r(g)$Pool
ThreadPool    N   )util)get_contextTimeoutError)waitINITRUNCLOSE	TERMINATEc                 $    [        [        U 6 5      $ N)listmapargss    +/usr/lib/python3.13/multiprocessing/pool.pymapstarr   /   s    T
    c                 N    [        [        R                  " U S   U S   5      5      $ )Nr   r   )r   	itertoolsstarmapr   s    r   starmapstarr   2   s"    	!!$q'47344r   c                        \ rS rSrS rS rSrg)RemoteTraceback9   c                     Xl         g r   tb)selfr    s     r   __init__RemoteTraceback.__init__:   s    r   c                     U R                   $ r   r   r!   s    r   __str__RemoteTraceback.__str__<   s    wwr   r   N)__name__
__module____qualname____firstlineno__r"   r&   __static_attributes__ r   r   r   r   9   s    r   r   c                        \ rS rSrS rS rSrg)ExceptionWithTraceback?   c                     [         R                  " [        U5      X5      nSR                  U5      nXl        SU-  U l        g )N z

"""
%s""")	tracebackformat_exceptiontypejoinexcr    )r!   r7   r    s      r   r"   ExceptionWithTraceback.__init__@   s6    ''S	3;WWR[ 2%r   c                 >    [         U R                  U R                  44$ r   )rebuild_excr7   r    r%   s    r   
__reduce__!ExceptionWithTraceback.__reduce__E   s    TXXtww///r   r7   r    N)r(   r)   r*   r+   r"   r;   r,   r-   r   r   r/   r/   ?   s    &
0r   r/   c                 &    [        U5      U l        U $ r   )r   	__cause__r=   s     r   r:   r:   H   s    #B'CMJr   c                   8   ^  \ rS rSrSrU 4S jrS rS rSrU =r	$ )MaybeEncodingErrorP   zRWraps possible unpickleable errors, so they can be
safely sent through the socket.c                    > [        U5      U l        [        U5      U l        [        [        U ]  U R                  U R                  5        g r   )reprr7   valuesuperrA   r"   )r!   r7   rE   	__class__s      r   r"   MaybeEncodingError.__init__T   s3    9%[
 $04::Fr   c                 @    SU R                   < SU R                  < S3$ )NzError sending result: 'z'. Reason: '')rE   r7   r%   s    r   r&   MaybeEncodingError.__str__Y   s    =AZZ=AXXG 	Gr   c                 @    SU R                   R                  < SU < S3$ )N<z: >)rG   r(   r%   s    r   __repr__MaybeEncodingError.__repr__]   s    !^^44d;;r   )r7   rE   )
r(   r)   r*   r+   __doc__r"   r&   rO   r,   __classcell__rG   s   @r   rA   rA   P   s    'G
G< <r   rA   c                    Ub5  [        U[        5      (       a  US:  d  [        SR                  U5      5      eUR                  nU R
                  n[        U S5      (       a4  U R                  R                  5         UR                  R                  5         Ub  U" U6   SnUb  U(       af  X:  aa   U" 5       n	U	c  [        R                  " S5        O?U	u  ppn SU" U0 UD64n U" XU45        S =n	=n
=n=n=pUS-  nUc  MS  U(       a  X:  a  Ma  [        R                  " S
U-  5        g ! [        [        4 a    [        R                  " S5         MC  f = f! [         a4  nU(       a  U[        La  [!        UUR"                  5      nSU4n S nANS nAff = f! [         a>  n[%        UUS   5      n[        R                  " S	U-  5        U" XSU445         S nANS nAff = f)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hasattrrU   close_readerEOFErrorOSErrorr   debug	Exception_helper_reraises_exceptionr/   __traceback__rA   )inqueueoutqueueinitializerinitargsmaxtaskswrap_exceptionrZ   r[   	completedtaskjobifuncr   kwdsresultewrappeds                     r   workerrt   a   s   z(C'@'@+3q=9@@JKK
,,C
++Cw	"" XI

xI,@	5D
 <JJ78#' D	 D$/$/0F
	, ! 4877s7V7d7TQ	7 
xI,@8 	JJ.:;3 '" 	JJBC	  	 $.H"H*1aoo>QZF	   	,(F1I6GJJI %)*++		,sB   0D* 
E #
F *&EE
F!*FF
G "4GG c                     U e)z@Pickle-able helper function for use by _guarded_task_generation.r-   )exs    r   rc   rc      s    
Hr   c                   @   ^  \ rS rSrSrSS.U 4S jjrU 4S jrSrU =r$ )
_PoolCache   z
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               2   > Xl         [        TU ]  " U0 UD6  g r   )r{   rF   r"   )r!   r{   r   rp   rG   s       r   r"   _PoolCache.__init__   s     $'$'r   c                 j   > [         TU ]  U5        U (       d  U R                  R                  S 5        g g r   )rF   __delitem__r{   rZ   )r!   itemrG   s     r   r   _PoolCache.__delitem__   s,    D! MMd# r   )	r(   r)   r*   r+   rQ   r"   r   r,   rR   rS   s   @r   rx   rx      s!     +/ ( (
$ 
$r   rx   c                      \ rS rSrSrSr\S 5       r  S*S jr\	R                  \4S jrS	 rS
 r\S 5       r\S 5       rS r\S 5       r\S 5       rS rS rS0 4S jrS+S jrS+S jr  S,S jrS rS-S jrS-S jrS0 SS4S jr  S,S jr  S,S jr\S+S j5       r \!S 5       r"\S 5       r#\S 5       r$\S  5       r%S! r&S" r'S# r(S$ r)\S% 5       r*\!S& 5       r+S' r,S( r-S)r.g).r      zK
Class which supports an async version of applying functions to arguments.
Tc                 &    U R                   " U0 UD6$ r   Process)ctxr   rp   s      r   r   Pool.Process   s    {{D)D))r   Nr-   c                    / U l         [        U l        U=(       d
    [        5       U l        U R                  5         [        R                  " 5       U l        U R                  R                  5       U l	        [        U R                  S9U l        X@l        X l        X0l        Uc  [        R                   " 5       =(       d    SnUS:  a  [#        S5      eUb&  [%        U[&        5      (       a  US::  a  [#        S5      eUb  [)        U5      (       d  [+        S5      eXl         U R/                  5         U R9                  5       n[:        R<                  " [>        R@                  U R                  U R                  U R                  U RB                  U R,                  U R                   U RD                  U RF                  U R                  U R                  U R                  U RH                  XpR                  4S9U l%        SU RJ                  l&        [N        U RJ                  l        U RJ                  RQ                  5         [:        R<                  " [>        RR                  U R                  U RT                  U RF                  U R                   U R                  4S9U l+        SU RV                  l&        [N        U RV                  l        U RV                  RQ                  5         [:        R<                  " [>        RX                  U RF                  U RZ                  U R                  4S9U l.        SU R\                  l&        [N        U R\                  l        U R\                  RQ                  5         [^        R`                  " X Rb                  U R                  U RD                  U RF                  U R                   U R                  U RJ                  U RV                  U R\                  U R                  4	S	S
9U l2        [N        U l        g ! [0         aW    U R                    H"  nUR2                  b  M  UR5                  5         M$     U R                    H  nUR7                  5         M     e f = f)Nrz   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_notifierrx   _cache_maxtasksperchild_initializer	_initargsosprocess_cpu_count
ValueErrorrV   rW   callable	TypeError
_processes_repopulate_poolrb   exitcode	terminater6   _get_sentinels	threadingThreadr   _handle_workersr   _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!   	processesrg   rh   maxtasksperchildcontextp	sentinelss           r   r"   Pool.__init__   sI    
,{}	++- !%		 5 5 7 $*?*?@!1'!,,.3!Iq=EFF'.448HA8M !RSS"8K+@+@<==#	!!# '')	(//''++t		4<<//4::t}}dnn##T^^T5K5K&&	3H3HJ  '+#&)#""$ '--%%//4??DNN**dkk+
 %)!$'!  "(//''..$//4;;?  '+#&)#""$--&&//4==$..$**'')=)=t?Q?Q&&5  ]  	ZZ::%KKM   ZZ  	s   O1 1$Q9Qc                     U R                   U:X  a;  U" SU < 3[        U S9  [        U SS 5      b  U R                  R	                  S 5        g g g )Nz&unclosed running multiprocessing pool )sourcer   )r   ResourceWarninggetattrr   rZ   )r!   _warnr   s      r   __del__Pool.__del__
  sT    ;;#:4(C!$0t/6B%%))$/ C r   c           
          U R                   nSUR                   SUR                   SU R                   S[	        U R
                  5       S3	$ )NrM   .z state=z pool_size=rN   )rG   r)   r*   r   lenr   )r!   clss     r   rO   Pool.__repr__  sQ    nnCNN#1S%5%5$6 7 & _-Q0 	1r   c                 j    U R                   R                  /nU R                  R                  /n/ UQUQ$ r   )r   r^   r   )r!   task_queue_sentinelsself_notifier_sentinelss      r   r   Pool._get_sentinels  s<     $ 6 67#'#8#8#@#@"A@%@(?@@r   c                 l    U  Vs/ s H"  n[        US5      (       d  M  UR                  PM$     sn$ s  snf )Nsentinel)r\   r   )workersrt   s     r   _get_worker_sentinelsPool._get_worker_sentinels  s<     8 %+"6:6  8 	8 8s   11c                     Sn[        [        [        U 5      5      5       HC  nX   nUR                  c  M  [        R
                  " SU-  5        UR                  5         SnX	 ME     U$ )zCleanup after any worker processes which have exited due to reaching
their specified lifetime.  Returns True if any workers were cleaned up.
Fcleaning up worker %dT)reversedranger   r   r   ra   r6   )poolcleanedrn   rt   s       r   _join_exited_workersPool._join_exited_workers!  s^    
 %D	*+AWF*

2Q67G , r   c                     U R                  U R                  U R                  U R                  U R                  U R
                  U R                  U R                  U R                  U R                  U R                  5
      $ r   )_repopulate_pool_staticr   r   r   r   r   r   r   r   r   r   r%   s    r   r   Pool._repopulate_pool1  s_    ++DIIt||,0OO,0JJ,0NND<M<M,0NN,0,B,B,0,@,@B 	Br   c
                    [        U[        U5      -
  5       Hr  n
U" U [        XEUXxU	4S9nUR                  R	                  SS5      Ul        SUl        UR                  5         UR                  U5        [        R                  " S5        Mt     g)zpBring the number of pool processes up to the specified number,
for use after reaping workers which have exited.
r   r   
PoolWorkerTzadded workerN)
r   r   rt   namereplacer   r   appendr   ra   )r   r   r   r   re   rf   rg   rh   r   rj   rn   ws               r   r   Pool._repopulate_pool_static:  sz     y3t9,-AF%)&,./A
 VV^^I|<AFAHGGIKKNJJ~& .r   c
                 n    [         R                  U5      (       a  [         R                  XX#XEUXxU	5
        gg)zEClean up any exited workers and start replacements for them.
        N)r   r   r   )
r   r   r   r   re   rf   rg   rh   r   rj   s
             r   _maintain_poolPool._maintain_poolM  s7     $$T**((y)0K)1)79 +r   c                    U R                   R                  5       U l        U R                   R                  5       U l        U R                  R                  R
                  U l        U R                  R                  R                  U l	        g r   )
r   r   r   r   rU   sendr   r^   recvr   r%   s    r   r   Pool._setup_queuesY  sX    		--/..0--//44..0055r   c                 B    U R                   [        :w  a  [        S5      eg )NzPool not running)r   r   r   r%   s    r   _check_runningPool._check_running_  s    ;;#/00 r   c                 B    U R                  XU5      R                  5       $ )z<
Equivalent of `func(*args, **kwds)`.
Pool must be running.
)apply_asyncr[   )r!   ro   r   rp   s       r   apply
Pool.applyc  s     
 D15577r   c                 L    U R                  X[        U5      R                  5       $ )z`
Apply `func` to each element in `iterable`, collecting the results
in a list that is returned.
)
_map_asyncr   r[   r!   ro   iterable	chunksizes       r   r   Pool.mapj  s     
 tw	BFFHHr   c                 L    U R                  X[        U5      R                  5       $ )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   r[   r   s       r   r   Pool.starmapq  s      t{IFJJLLr   c                 2    U R                  X[        UXE5      $ )z-
Asynchronous version of `starmap()` method.
)r   r   r!   ro   r   r   callbackerror_callbacks         r   starmap_asyncPool.starmap_asyncy  s    
 t{I'9 	9r   c              #      #     Sn[        U5       H  u  pEXX%40 4v   M     g! [         a  nUWS-   [        U40 4v    SnAgSnAff = f7f)zProvides a generator of tasks for imap and imap_unordered with
appropriate handling for iterables which throw exceptions during
iteration.r   N)	enumeraterb   rc   )r!   
result_jobro   r   rn   xrr   s          r   _guarded_task_generationPool._guarded_task_generation  sY     	JA!(+!dD"55 , 	Jqs$>bII	Js)   A# A
AAAAAc                    U R                  5         US:X  aN  [        U 5      nU R                  R                  U R	                  UR
                  X5      UR                  45        U$ US:  a  [        SR                  U5      5      e[        R                  XU5      n[        U 5      nU R                  R                  U R	                  UR
                  [        U5      UR                  45        S U 5       $ )z@
Equivalent of `map()` -- can be MUCH slower than `Pool.map()`.
r   zChunksize must be 1+, not {0:n}c              3   6   #    U  H  o  H  o"v   M     M     g 7fr   r-   .0chunkr   s      r   	<genexpr>Pool.imap.<locals>.<genexpr>       ?fUDDf   )r   IMapIteratorr   rZ   r  _job_set_lengthr   rY   r   
_get_tasksr   r!   ro   r   r   rq   task_batchess         r   imap	Pool.imap  s     	>!$'FOO11&++tN&&
 M1} 5<<!#$ $  ??49EL!$'FOO11&++292>@ &&	 @f??r   c                    U R                  5         US:X  aN  [        U 5      nU R                  R                  U R	                  UR
                  X5      UR                  45        U$ US:  a  [        SR                  U5      5      e[        R                  XU5      n[        U 5      nU R                  R                  U R	                  UR
                  [        U5      UR                  45        S U 5       $ )z<
Like `imap()` method but ordering of results is arbitrary.
r   zChunksize must be 1+, not {0!r}c              3   6   #    U  H  o  H  o"v   M     M     g 7fr   r-   r  s      r   r  &Pool.imap_unordered.<locals>.<genexpr>  r  r  )r   IMapUnorderedIteratorr   rZ   r  r  r  r   rY   r   r  r   r  s         r   imap_unorderedPool.imap_unordered  s     	>*40FOO11&++tN&&
 M1} 5<<YGI I??49EL*40FOO11&++292>@ &&	 @f??r   c                     U R                  5         [        XU5      nU R                  R                  UR                  SXU4/S45        U$ )z+
Asynchronous version of `apply()` method.
r   N)r   ApplyResultr   rZ   r  )r!   ro   r   rp   r   r   rq   s          r   r   Pool.apply_async  sH    
 	T^<v{{At4@A4HIr   c                 2    U R                  X[        X4U5      $ )z)
Asynchronous version of `map()` method.
)r   r   r   s         r   	map_asyncPool.map_async  s    
 tw	 	r   c                    U R                  5         [        US5      (       d  [        U5      nUc9  [        [	        U5      [	        U R
                  5      S-  5      u  pGU(       a  US-  n[	        U5      S:X  a  Sn[        R                  XU5      n[        X[	        U5      UUS9n	U R                  R                  U R                  U	R                  UU5      S45        U	$ )zI
Helper function to implement map, starmap and their async counterparts.
__len__N   r   r   r   )r   r\   r   divmodr   r   r   r  	MapResultr   rZ   r  r  )
r!   ro   r   mapperr   r   r   extrar  rq   s
             r   r   Pool._map_async  s    
 	x++H~H%c(mS_q5HIIQ	x=AItyA4CM8*8:--fkk.4.:< 		
 r   c                     [        XS9  UR                  5       (       d(  UR                  5         UR                  5       (       d  M'  g g )N)timeout)r	   emptyr[   )r   change_notifierr-  s      r   _wait_for_updatesPool._wait_for_updates  s6    Y(!''))! "''))r   c                    [         R                  " 5       nUR                  [        :X  d  U(       a  UR                  [        :w  ap  U R                  X4XVUXU
X5
        / U R                  U5      QUQnU R                  UU5        UR                  [        :X  a  MS  U(       a  UR                  [        :w  a  Mp  UR                  S 5        [        R                  " S5        g )Nzworker handler exiting)r   current_threadr   r   r   r   r   r0  rZ   r   ra   )r   cache	taskqueuer   r   r   r   re   rf   rg   rh   r   rj   r   r/  threadcurrent_sentinelss                    r   r   Pool._handle_workers  s    
 ))+ mms"u)1KsYg'h/A !O#";";D"A NI N!!"3_E mms"u)1K 	d

+,r   c                 f   [         R                  " 5       n[        U R                  S 5       H  u  pgS n U H8  nUR                  [
        :w  a  [        R                  " S5          OK U" U5        M:     U(       a/  [        R                  " S5        U(       a  US   OSnU" US-   5         S =n=pjM   S =n=pj  O   [        R                  " S5         [        R                  " S5        UR                  S 5        [        R                  " S	5        U H  nU" S 5        M     [        R                  " S5        g ! [         a@  n	US S u  p XJ   R                  USU	45         S n	A	GM/  ! [         a	      S n	A	GMA  f = fS n	A	ff = f! S =n=pjf = f! [         a    [        R                  " S
5         Nf = f)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   r3  iterr[   r   r   r   ra   rb   _setKeyErrorrZ   r`   )r5  rZ   rf   r   r4  r6  taskseq
set_lengthrl   rr   rm   idxr   s                r   r   Pool._handle_tasks  s   ))+#'	t#<GD,#D}}+

#LM!D	 $ "

#78)-d1g2"37+ (,++w '+++w1 $=4 JJ23
	JJJHILL JJABD	 
 	

)*= % !#'8!!JOOC%<<' ! !	! (,++w  	JJJHI	Jsl   1F&D6.:FAF 6
F  E;E%F%
E8	/E;0F7E8	8E;;F  FF
 F0/F0c                 n   [         R                  " 5       n  U" 5       nUR                  [        :w  a2  UR                  [        :X  d   S5       e[        R
                  " S5        O:Uc  [        R
                  " S5        O Uu  pVn X%   R                  Xg5        S =n=pWM  U(       ar  UR                  [        :w  a^   U" 5       nUc  [        R
                  " S5        M>  Uu  pVn X%   R                  Xg5        S =n=pWU(       a  UR                  [        :w  a  M^  [        U S5      (       aR  [        R
                  " S5         [        S5       H+  nU R                  R                  5       (       d    OU" 5         M-      [        R
                  " S	[        U5      UR                  5        g ! [        [        4 a    [        R
                  " S5         g f = f! [         a     GNHf = f! [        [        4 a    [        R
                  " S5         g f = f! [         a     GN'f = f! [        [        4 a     Nf = f)
Nz.result handler got EOFError/OSError -- exitingzThread not in TERMINATEz,result handler found thread._state=TERMINATEzresult handler got sentinelz&result handler ignoring extra sentinelr^   z"ensuring that outqueue is not full
   z7result handler exiting: len(cache)=%s, thread._state=%s)r   r3  r`   r_   r   ra   r   r   r   r<  r=  r\   r   r^   pollr   )rf   r[   r4  r6  rl   rm   rn   objs           r   r   Pool._handle_results=  s   ))+u
 }}#}}	1L3LL1

IJ|

89KCC
'  $#D#3+ . 2u
 |

CDKCC
'  $#D#3 2" 8Y''JJ;<rA#++0022E # 	

L%j&--	)e X& 

KL    X& 

KL   X& s_   F' G ;G$ #H  .H! /H! '&GG
G! G!$&HH
HH!H43H4c              #   ~   #    [        U5      n [        [        R                  " X5      5      nU(       d  g X4v   M.  7fr   )r;  tupler   islice)ro   itsizer  s       r   r  Pool._get_tasksy  s8     "Xi&&r01A)O	 s   ;=c                     [        S5      e)Nz:pool objects cannot be passed between processes or pickled)NotImplementedErrorr%   s    r   r;   Pool.__reduce__  s    !J 	r   c                     [         R                  " S5        U R                  [        :X  a<  [        U l        [        U R
                  l        U R                  R                  S 5        g g )Nzclosing pool)r   ra   r   r   r   r   r   rZ   r%   s    r   r]   
Pool.close  sH    

>";;#DK*/D  '!!%%d+ r   c                 f    [         R                  " S5        [        U l        U R	                  5         g )Nzterminating pool)r   ra   r   r   r   r%   s    r   r   Pool.terminate  s     

%&r   c                    [         R                  " S5        U R                  [        :X  a  [	        S5      eU R                  [
        [        4;  a  [	        S5      eU R                  R                  5         U R                  R                  5         U R                  R                  5         U R                   H  nUR                  5         M     g )Nzjoining poolzPool is still runningzIn unknown state)r   ra   r   r   r   r   r   r   r6   r   r   r   )r!   r   s     r   r6   	Pool.join  s    

>";;#455[[	 22/00!!#!!!#AFFH r   c                    [         R                  " S5        U R                  R                  5         UR	                  5       (       a  U R
                  R                  5       (       ah  U R
                  R                  5         [        R                  " S5        UR	                  5       (       a"  U R
                  R                  5       (       a  Mf  g g g g )Nz7removing tasks from inqueue until task handler finishedr   )
r   ra   _rlockacquireis_aliver^   rD  r   timesleep)re   task_handlerrK  s      r   _help_stuff_finishPool._help_stuff_finish  s     	

LM ##%%'//*>*>*@*@OO  "JJqM ##%%'//*>*>*@*@%*@%r   c
                    [         R                  " S5        [        Ul        UR	                  S 5        [        Ul        [         R                  " S5        U R                  X'[        U5      5        UR                  5       (       d  [        U	5      S:w  a  [        S5      e[        Ul        UR	                  S 5        UR	                  S 5        [         R                  " S5        [        R                  " 5       ULa  UR                  5         U(       aR  [        US   S5      (       a>  [         R                  " S5        U H"  n
U
R                  b  M  U
R                  5         M$     [         R                  " S5        [        R                  " 5       ULa  UR                  5         [         R                  " S	5        [        R                  " 5       ULa  UR                  5         U(       a  [        US   S5      (       aj  [         R                  " S
5        U HM  n
U
R                  5       (       d  M  [         R                  " SU
R                  -  5        U
R                  5         MO     g g g )Nzfinalizing poolz&helping task handler/workers to finishr   z/Cannot have cache with result_handler not alivezjoining worker handlerr   zterminating workerszjoining task handlerzjoining result handlerzjoining pool workersr   )r   ra   r   r   rZ   r]  r   rY  rX   r   r3  r6   r\   r   r   pid)r   r5  re   rf   r   r/  worker_handlerr\  result_handlerr4  r   s              r   r   Pool._terminate_pool  s    	

$%
 !*D!'

;<wc$i@''))E
a AC C !*D!T 	

+,##%^;! GDG[11JJ,-::%KKM  	

)*##%\9

+,##%^;!GDG[11JJ-.::<<JJ6>?FFH	  24r   c                 &    U R                  5         U $ r   )r   r%   s    r   	__enter__Pool.__enter__  s    r   c                 $    U R                  5         g r   )r   )r!   exc_typeexc_valexc_tbs       r   __exit__Pool.__exit__  s    r   )r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   )NNr-   NNr   )NNN)r   )/r(   r)   r*   r+   rQ   r   staticmethodr   r"   warningswarnr   r   rO   r   r   r   r   r   r   r   r   r   r   r   r   r  r  r  r   r!  r   r0  classmethodr   r   r   r  r;   r]   r   r6   r]  r   re  rk  r,   r-   r   r   r   r      s    O* * CE04Of %MMs 01A
 8 8  B ' '$ 	9 	961  " 8IM FJ9	J@:@8 &(b4 BF KO: " "
 - -( ++ ++Z 9) 9)v  
,

   3 3jr   c                   d    \ rS rSrS rS rS rS
S jrS
S jrS r	\
" \R                  5      rS	rg)r  i  c                     Xl         [        R                  " 5       U l        [	        [
        5      U l        UR                  U l        X l        X0l	        X R                  U R                  '   g r   )
r   r   Event_eventnextjob_counterr  r   	_callback_error_callback)r!   r   r   r   s       r   r"   ApplyResult.__init__  sG    
oo'%	kk!-!%DIIr   c                 6    U R                   R                  5       $ r   )rt  is_setr%   s    r   readyApplyResult.ready  s    {{!!##r   c                 x    U R                  5       (       d  [        SR                  U 5      5      eU R                  $ )Nz{0!r} not ready)r|  r   rY   _successr%   s    r   
successfulApplyResult.successful  s.    zz||.55d;<<}}r   Nc                 :    U R                   R                  U5        g r   )rt  r	   r!   r-  s     r   r	   ApplyResult.wait  s    !r   c                     U R                  U5        U R                  5       (       d  [        eU R                  (       a  U R                  $ U R                  er   )r	   r|  r   r  _valuer  s     r   r[   ApplyResult.get  s:    		'zz||==;;++r   c                    Uu  U l         U l        U R                  (       a,  U R                   (       a  U R                  U R                  5        U R                  (       a,  U R                   (       d  U R                  U R                  5        U R                  R                  5         U R                  U R                  	 S U l        g r   )	r  r  rw  rx  rt  setr   r  r   r!   rn   rE  s      r   r<  ApplyResult._set  ss    %("t{>>dmmNN4;;'  -KK		"
r   )r   rw  rx  rt  r  r   r  r  r   )r(   r)   r*   r+   r"   r|  r  r	   r[   r<  rp  typesGenericAlias__class_getitem__r,   r-   r   r   r  r    s3    &$
" $E$6$67r   r  c                        \ rS rSrS rS rSrg)r(  i  c                 
   [         R                  XUUS9  SU l        S /U-  U l        X l        US::  a9  SU l        U R                  R                  5         U R                  U R                  	 g X2-  [        X2-  5      -   U l        g )Nr&  Tr   )r  r"   r  r  
_chunksize_number_leftrt  r  r   r  bool)r!   r   r   lengthr   r   s         r   r"   MapResult.__init__  sz    T,: 	 	<fvo#> !DKKOODII& & 1D9K4L LDr   c                    U =R                   S-  sl         Uu  p4U(       a  U R                  (       a  X@R                  XR                  -  US-   U R                  -  & U R                   S:X  ae  U R                  (       a  U R	                  U R                  5        U R
                  U R                  	 U R                  R                  5         S U l	        g g U(       d  U R                  (       a  SU l        X@l        U R                   S:X  ae  U R                  (       a  U R                  U R                  5        U R
                  U R                  	 U R                  R                  5         S U l	        g g )Nr   r   F)r  r  r  r  rw  r   r  rt  r  r   rx  )r!   rn   success_resultsuccessrq   s        r   r<  MapResult._set)  s    Q(t}}CIKK//)1Q3*?@  A%>>NN4;;/KK		*!!
 & t}} %$  A%''((5KK		*!!
 &r   )r  r  r   r  r  N)r(   r)   r*   r+   r"   r<  r,   r-   r   r   r(  r(    s    M"r   r(  c                   :    \ rS rSrS rS rS	S jr\rS rS r	Sr
g)
r  iE  c                 D   Xl         [        R                  " [        R                  " 5       5      U l        [        [        5      U l        UR                  U l        [        R                  " 5       U l        SU l        S U l        0 U l        X R                  U R                  '   g )Nr   )r   r   	ConditionLock_condru  rv  r  r   collectionsdeque_items_index_length	_unsorted)r!   r   s     r   r"   IMapIterator.__init__G  sl    
(()9:
%	kk!'')!%DIIr   c                     U $ r   r-   r%   s    r   __iter__IMapIterator.__iter__R  s    r   Nc                    U R                       U R                  R                  5       nS S S 5        Wu  p4U(       a  U$ Ue! [         a    U R                  U R
                  :X  a  S U l        [        S eU R                   R                  U5         U R                  R                  5       n N! [         a0    U R                  U R
                  :X  a  S U l        [        S e[        S ef = ff = f! , (       d  f       N= fr   )
r  r  popleft
IndexErrorr  r  r   StopIterationr	   r   )r!   r-  r   r  rE   s        r   ru  IMapIterator.nextU  s    ZZ1{{**,   L!  1;;$,,.!%DJ'T1

(1;;..0D! 1{{dll2%)
+5&D0	11 Zs5   C+A  AC(B*(C+*:C$$C((C++
C9c                    U R                      U R                  U:X  a  U R                  R                  U5        U =R                  S-  sl        U R                  U R                  ;   aq  U R                  R                  U R                  5      nU R                  R                  U5        U =R                  S-  sl        U R                  U R                  ;   a  Mq  U R                   R                  5         OX R                  U'   U R                  U R                  :X  a  U R                  U R                  	 S U l
        S S S 5        g ! , (       d  f       g = fNr   )r  r  r  r   r  popnotifyr  r   r  r   r  s      r   r<  IMapIterator._setm  s    ZZ{{a""3'q kkT^^3..,,T[[9CKK&&s+KK1$K kkT^^3 

!!#$'q!{{dll*KK		*!
 ZZs   C
EA!E
Ec                     U R                      Xl        U R                  U R                  :X  a8  U R                   R                  5         U R                  U R
                  	 S U l        S S S 5        g ! , (       d  f       g = fr   )r  r  r  r  r   r  r   )r!   r  s     r   r  IMapIterator._set_length~  sO    ZZ!L{{dll*

!!#KK		*!
 ZZs   AA//
A=)r   r  r  r  r  r  r   r  r   )r(   r)   r*   r+   r"   r  ru  __next__r<  r  r,   r-   r   r   r  r  E  s"    	&, H"""r   r  c                       \ rS rSrS rSrg)r  i  c                 T   U R                      U R                  R                  U5        U =R                  S-  sl        U R                   R	                  5         U R                  U R
                  :X  a  U R                  U R                  	 S U l        S S S 5        g ! , (       d  f       g = fr  )	r  r  r   r  r  r  r   r  r   r  s      r   r<  IMapUnorderedIterator._set  sj    ZZKKs#KK1KJJ{{dll*KK		*!
 ZZs   BB
B')r   N)r(   r)   r*   r+   r<  r,   r-   r   r   r  r    s    "r   r  c                   d    \ rS rSrSr\S 5       rSS jrS rS r	\S 5       r
\S	 5       rS
 rSrg)r   i  Fc                     SSK Jn  U" U0 UD6$ )Nr   r   )dummyr   )r   r   rp   r   s       r   r   ThreadPool.Process  s    "%%%r   Nc                 0    [         R                  XX#5        g r   )r   r"   )r!   r   rg   rh   s       r   r"   ThreadPool.__init__  s    d{=r   c                     [         R                  " 5       U l        [         R                  " 5       U l        U R                  R                  U l        U R                  R                  U l        g r   )r   r   r   r   rZ   r   r[   r   r%   s    r   r   ThreadPool._setup_queues  sD    ))+**,--++..,,r   c                 0    U R                   R                  /$ r   )r   r^   r%   s    r   r   ThreadPool._get_sentinels  s    %%--..r   c                     / $ r   r-   )r   s    r   r    ThreadPool._get_worker_sentinels  s    	r   c                       U R                  SS9  M  ! [        R                   a     Of = f[        U5       H  nU R	                  S 5        M     g )NF)block)r[   r   Emptyr   rZ   )re   r\  rK  rn   s       r   r]  ThreadPool._help_stuff_finish  sM    	%( {{ 		tAKK s    ++c                 0    [         R                  " U5        g r   )rZ  r[  )r!   r   r/  r-  s       r   r0  ThreadPool._wait_for_updates  s    

7r   )r   r   r   r   )NNr-   )r(   r)   r*   r+   r   rm  r   r"   r   r   r   r]  r0  r,   r-   r   r   r   r     sU    O& &>-/    r   )Nr-   NF))__all__r  r   r   r   r   rZ  r3   r  rn  r2   r   r   r   
connectionr	   r
   r   r   r   countrv  r   r   rb   r   r/   r:   rA   rt   rc   dictrx   objectr   r  AsyncResultr(  r  r  r   r-   r   r   <module>r     s   <
    	        '  	 oo5i 0 0< <" GK+<Z$ $.v6 vx)8& )8V %" %"V?"6 ?"J	"L 	"$ $r   