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

"""
%s""")	tracebackformat_exceptiontypejoinexcr   )r    r1   r   s      r   r!   zExceptionWithTraceback.__init__@   s:    ''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    TXXtww///r   N)r%   r&   r'   r!   r4   r(   r   r   r*   r*   ?   s    &
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                         y r   )reprr1   valuesuperr8   r!   )r    r1   r;   	__class__s      r   r!   zMaybeEncodingError.__init__T   s3    9%[
 $04::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    =AZZ=AXXG 	Gr   c                 <    d| j                   j                  d| dS )N<z: >)r=   r%   r#   s    r   __repr__zMaybeEncodingError.__repr__]   s    !^^44d;;r   )r%   r&   r'   __doc__r!   r$   rC   __classcell__r=   s   @r   r8   r8   P   s    'G
G<r   r8   c                 |   |/t        |t              r|dk\  st        dj                  |            |j                  }| j
                  }t        | d      r4| j                  j                          |j                  j                          | ||  d}||r`||k  r[	  |       }	|	t        j                  d       n;|	\  }
}}}}	 d ||i |f}	  ||
||f       d x}	x}
x}x}x}}|dz  }|S|r||k  r[t        j                  d
|z         y # t        t        f$ r t        j                  d       Y ?w xY w# t        $ r.}|r|t        urt!        ||j"                        }d|f}Y d }~d }~ww xY w# t        $ r>}t%        ||d         }t        j                  d	|z          ||
|d|ff       Y d }~d }~ww xY w)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KK
,,C
++Cw	" XI

xI,@	5D
 <JJ78#' QdD	 D$/$/0F
	,a ! 4877s7V7d7TDQ	7 
xI,@8 	JJ.:;3 '" 	JJBC	  	 $.H"H*1aoo>QZF	   	,(F1I6GJJI a%)*++		,sB   D 
D: E4 $D76D7:	E1$E,,E14	F;=4F66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               2    || _         t        |   |i | y r   )rm   r<   r!   )r    rm   r   rc   r=   s       r   r!   z_PoolCache.__init__   s     $'$'r   c                 `    t         |   |       | s| j                  j                  d        y y r   )r<   __delitem__rm   rM   )r    itemr=   s     r   rp   z_PoolCache.__delitem__   s,    D! MMd# r   )r%   r&   r'   rD   r!   rp   rE   rF   s   @r   rk   rk      s     +/ (
$ 
$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,y),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        | _        |xs
 t               | _        | j                          t        j                         | _        | j                  j                         | _	        t        | j                        | _        || _        || _        || _        |t        j                          xs d}|dk  rt#        d      | t%        |t&              r|dk  rt#        d      |t)        |      st+        d      || _        	 | j/                          | j9                         }t;        j<                  t>        j@                  | j                  | j                  | j                  | jB                  | j,                  | j                   | jD                  | jF                  | j                  | j                  | j                  | jH                  || j                  f      | _%        d| jJ                  _&        tN        | jJ                  _        | jJ                  jQ                          t;        j<                  t>        jR                  | j                  | jT                  | jF                  | j                   | j                  f      | _+        d| jV                  _&        tN        | jV                  _        | jV                  jQ                          t;        j<                  t>        jX                  | jF                  | jZ                  | j                  f      | _.        d| j\                  _&        tN        | j\                  _        | j\                  jQ                          t_        j`                  | | jb                  | j                  | jD                  | jF                  | j                   | j                  | jJ                  | jV                  | j\                  | j                  f	d	
      | _2        tN        | _        y # t0        $ rQ | j                   D ]  }|j2                  |j5                          ! | j                   D ]  }|j7                            w xY w)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__   sV    
,{}	++- !%		 5 5 7 $*?*?@!1'!+!Iq=EFF'.48HA8M !RSS"8K+@<==#	!!# '')	(//''++t		4<<//4::t}}dnn##T^^T5K5K&&	43H3HJ  '+#&)#""$ '--%%//4??DNN**dkk+
 %)!$'!  "(//''..$//4;;?  '+#&)#""$--$&&//4==$..$**'')=)=t?Q?Q&&5  ]  	ZZ "::%KKM" ZZ 	s   
O' '$Q5Qc                     | j                   |k(  r< |d| t        |        t        | dd       | j                  j	                  d        y y y )Nz&unclosed running multiprocessing pool )sourcer   )r}   ResourceWarninggetattrr   rM   )r    _warnr   s      r   __del__zPool.__del__
  sT    ;;#:4(C!$0t/6B%%))$/ C r   c           
          | 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__  sQ    nnCNN#1S%5%5$6 7 & _-Q0 	1r   c                 j    | 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  s<     $ 6 67#'#8#8#@#@"A@%@(?@@r   c                 X    | D cg c]  }t        |d      r|j                   c}S c c}w )Nsentinel)rO   r   )workersrg   s     r   _get_worker_sentinelszPool._get_worker_sentinels  s1     8F"6:6  8 	8 8s   'c                     d}t        t        t        |                   D ]A  }| |   }|j                  t	        j
                  d|z         |j                          d}| |= C |S )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   rT   r0   )poolcleanedra   rg   s       r   _join_exited_workerszPool._join_exited_workers!  sg    
 %D	*+ 	A!WF*

2Q67G	 r   c                     | j                  | 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  s_    ++DIIt||,0OO,0JJ,0NND<M<M,0NN,0,B,B,0,@,@B 	Br   c
                 "   t        |t        |      z
        D ]u  }
 || t        ||||||	f      }|j                  j	                  dd      |_        d|_        |j                          |j                  |       t        j                  d       w y)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     y3t9,- 
	'AF%x)&(8,./A
 VV^^I|<AFAHGGIKKNJJ~&
	'r   c
                 l    t         j                  |      rt         j                  | |||||||||	
       yy)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*((gy$)0(K)13C)79 +r   c                    | j                   j                         | _        | j                   j                         | _        | j                  j                  j
                  | _        | j                  j                  j                  | _	        y r   )
r~   r   r   r   rH   sendr   rQ   recvr   r#   s    r   r   zPool._setup_queuesY  sX    		--/..0--//44..0055r   c                 @    | j                   t        k7  rt        d      y )NzPool not running)r}   r   r   r#   s    r   _check_runningzPool._check_running_  s    ;;#/00 r   c                 D    | j                  |||      j                         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5577r   c                 N    | j                  ||t        |      j                         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FFHHr   c                 N    | j                  ||t        |      j                         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JJLLr   c                 6    | j                  ||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 	9r   c              #      K   	 d}t        |      D ]  \  }}||||fi f  y# t        $ r}|dz   t        |fi f Y d}~yd}~ww xY w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  se     	JA!(+ 61!1dQD"556 	Jqs$>bII	Js)   A$ A	AAAAAc                    | j                          |dk(  rOt        |       }| j                  j                  | j	                  |j
                  ||      |j                  f       |S |dk  rt        dj                  |            t        j                  |||      }t        |       }| j                  j                  | 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 ]  }|   y wr   r(   .0chunkrq   s      r   	<genexpr>zPool.imap.<locals>.<genexpr>       ?U?D?D?   )r   IMapIteratorr   rM   r   _job_set_lengthr   rL   r   
_get_tasksr   r    rb   r   r   rd   task_batchess         r   imapz	Pool.imap  s     	>!$'FOO11&++tXN&&
 M1} 5<<!#$ $  ??49EL!$'FOO11&++292>@ &&	 @f??r   c                    | j                          |dk(  rOt        |       }| j                  j                  | j	                  |j
                  ||      |j                  f       |S |dk  rt        dj                  |            t        j                  |||      }t        |       }| j                  j                  | 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 ]  }|   y w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0FOO11&++tXN&&
 M1} 5<<YGI I??49EL*40FOO11&++292>@ &&	 @f??r   c                     | j                          t        | ||      }| j                  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  sL    
 	T8^<v{{AtT4@A4HIr   c                 6    | j                  ||t        |||      S )z9
        Asynchronous version of `map()` method.
        )r   r   r   s         r   	map_asynczPool.map_async  s"    
 tXw	8 	r   c                    | j                          t        |d      st        |      }|5t        t	        |      t	        | j
                        dz        \  }}|r|dz  }t	        |      dk(  rd}t        j                  |||      }t        | |t	        |      ||      }	| j                  j                  | 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(mS_q5HIIuQ	x=AItXyA4CM8*8:--fkk.4.:< 		
 r   c                     t        | |       |j                         s"|j                          |j                         s!y y )N)timeout)r	   emptyrN   )r   change_notifierr  s      r   _wait_for_updateszPool._wait_for_updates  s2    Y(!'')! "'')r   c                    t        j                         }|j                  t        k(  s|r~|j                  t        k7  rk| j                  |||||||	|
||
       g | j                  |      |}| j                  ||       |j                  t        k(  rU|r|j                  t        k7  rk|j                  d        t        j                  d       y )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    
 ))+ mms"u)1KsGYg'h/A !O#";";D"A NI N!!"3_E mms"u)1K 	d

+,r   c                 0   t        j                         }t        | j                  d       D ]}  \  }}d }	 |D ]5  }|j                  t
        k7  rt        j                  d        n>	  ||       7 |r)t        j                  d       |r|d   nd} ||dz          	 d x}x}}
v	 d x}x}}
 n t        j                  d       	 t        j                  d       |j                  d        t        j                  d	       |D ]
  } |d         	 t        j                  d       y # t        $ r;}	|d d \  }
}	 ||
   j                  |d|	f       n# t        $ r Y nw xY wY d }	~	+d }	~	ww xY w# d x}x}}
w xY w# t        $ r t        j                  d
       Y w xY w)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  s   ))+#'	t#< 	4GZD,# D}}+

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

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

)*= % !#'8S!!#JOOC%<' ! !	! (,++w  	JJJHI	Jsl   /E)$D",-E)A
E4 "	E&+E!4EE!	E	E!E	E!E)!E&&E))E14FFc                 $   t        j                         }	 	  |       }|j                  t        k7  r0|j                  t        k(  sJ d       t	        j
                  d       n;|t	        j
                  d       n#|\  }}}	 ||   j                  ||       d x}x}}|rk|j                  t        k7  rX	  |       }|t	        j
                  d       5|\  }}}	 ||   j                  ||       d x}x}}|r|j                  t        k7  rXt        | d      rIt	        j
                  d       	 t        d      D ]%  }| j                  j                         s n	 |        ' t	        j
                  d	t        |      |j                         y # t        t        f$ r t	        j
                  d       Y y w xY w# t        $ r Y *w xY w# t        t        f$ r t	        j
                  d       Y y w xY w# t        $ r Y w xY w# t        t        f$ r Y w xY w)
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 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   ))+u
 }}#}}	1L3LL1

IJ|

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

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

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

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

KL   X& sY   F	  F3 2G G- +3G= 	$F0/F03	G ?G $G*)G*-	G:9G:=HHc              #   t   K   t        |      }	 t        t        j                  ||            }|sy | |f )wr   )r  tupler   islice)rb   itsizer   s       r   r   zPool._get_tasksy  s=     "Xi&&r401A)O	 s   68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(  r<t        | _        t        | j
                  _        | j                  j                  d        y y )Nzclosing pool)r   rT   r}   r   r   r   r   rM   r#   s    r   rP   z
Pool.close  sH    

>";;#DK*/D  '!!%%d+ r   c                 d    t        j                  d       t        | _        | j	                          y )Nzterminating pool)r   rT   r   r}   r   r#   s    r   r   zPool.terminate  s     

%&r   c                    t        j                  d       | j                  t        k(  rt	        d      | j                  t
        t        fvrt	        d      | j                  j                          | j                  j                          | j                  j                          | j                  D ]  }|j                           y )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[[	 22/00!!#!!!# 	AFFH	r   c                 p   t        j                  d       | j                  j                          |j	                         rw| j
                  j                         r\| j
                  j                          t        j                  d       |j	                         r| j
                  j                         rZy y y y )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 ##%'//*>*>*@OO  "JJqM ##%'//*>*>*@%*@%r   c
                 d   t        j                  d       t        |_        |j	                  d        t        |_        t        j                  d       | j                  ||t        |             |j                         st        |	      dk7  rt        d      t        |_        |j	                  d        |j	                  d        t        j                  d       t        j                         |ur|j                          |rHt        |d   d      r9t        j                  d       |D ]  }
|
j                  |
j                          ! t        j                  d       t        j                         |ur|j                          t        j                  d	       t        j                         |ur|j                          |rpt        |d   d      r`t        j                  d
       |D ]E  }
|
j                         st        j                  d|
j                  z         |
j                          G y y y )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   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  s    	

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

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

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

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

+,##%^;!GDG[1JJ-. ::<JJ6>?FFH	 24r   c                 &    | j                          | S r   )r   r#   s    r   	__enter__zPool.__enter__  s    r   c                 $    | j                          y 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   r1  r6  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                   X    e Zd Zd Zd Zd ZddZddZd Z e	e
j                        Zy)	r   c                     || _         t        j                         | _        t	        t
              | _        |j                  | _        || _        || _	        | | j                  | j                  <   y r   )
r|   r   Event_eventnextjob_counterr   r   	_callback_error_callback)r    r   r   r   s       r   r!   zApplyResult.__init__  sO    
oo'%	kk!-!%DIIr   c                 6    | j                   j                         S r   )r>  is_setr#   s    r   readyzApplyResult.ready  s    {{!!##r   c                 n    | j                         st        dj                  |             | j                  S )Nz{0!r} not ready)rE  r   rL   _successr#   s    r   
successfulzApplyResult.successful  s,    zz|.55d;<<}}r   Nc                 :    | j                   j                  |       y r   )r>  r	   r    r  s     r   r	   zApplyResult.wait  s    !r   c                     | j                  |       | j                         st        | j                  r| j                  S | j                  r   )r	   rE  r   rG  _valuerJ  s     r   rN   zApplyResult.get  s8    		'zz|==;;++r   c                 ^   |\  | _         | _        | j                  r'| j                   r| j                  | j                         | j                  r'| j                   s| j                  | j                         | j                  j                          | j                  | j                  = d | _        y r   )	rG  rL  rA  rB  r>  setr   r   r|   r    ra   r  s      r   r  zApplyResult._set  ss    %("t{>>dmmNN4;;'  -KK		"
r   r   )r%   r&   r'   r!   rE  rH  r	   rN   r  r:  typesGenericAlias__class_getitem__r(   r   r   r   r     s3    &$
" $E$6$67r   r   c                       e Zd Zd Zd Zy)r   c                    t         j                  | |||       d| _        d g|z  | _        || _        |dk  r9d| _        | j                  j                          | j                  | j                  = y ||z  t        ||z        z   | _        y )Nr   Tr   )r   r!   rG  rL  
_chunksize_number_leftr>  rN  r   r   bool)r    r   r   lengthr   r   s         r   r!   zMapResult.__init__  s    T4,: 	 	<fvo#> !DKKOODII& &	 1D)9K4L LDr   c                    | xj                   dz  c_         |\  }}|r| j                  r|| j                  || j                  z  |dz   | j                  z   | j                   dk(  r`| j                  r| j	                  | j                         | j
                  | j                  = | j                  j                          d | _	        y y |s| j                  rd| _        || _        | j                   dk(  r`| j                  r| j                  | j                         | j
                  | j                  = | j                  j                          d | _	        y y )Nr   r   F)rV  rG  rL  rU  rA  r   r   r>  rN  r|   rB  )r    ra   success_resultsuccessrd   s        r   r  zMapResult._set)  s   Q(t}}CIDKK$//)1Q3*?@  A%>>NN4;;/KK		*!!
 & t}} %$  A%''((5KK		*!!
 &r   N)r%   r&   r'   r!   r  r(   r   r   r   r     s    M"r   r   c                   0    e Zd Zd Zd ZddZeZd Zd Zy)r   c                 B   || _         t        j                  t        j                               | _        t        t              | _        |j                  | _        t        j                         | _        d| _        d | _        i | _        | | j                  | j                  <   y )Nr   )r|   r   	ConditionLock_condr?  r@  r   r   collectionsdeque_items_index_length	_unsorted)r    r   s     r   r!   zIMapIterator.__init__G  sp    
(()9:
%	kk!'')!%DIIr   c                     | S r   r(   r#   s    r   __iter__zIMapIterator.__iter__R  s    r   Nc                    | j                   5  	 | j                  j                         }d d d        \  }}|r|S |# t        $ r | j                  | j
                  k(  rd | _        t        d | j                   j                  |       	 | j                  j                         }n;# t        $ r/ | j                  | j
                  k(  rd | _        t        d t        d w xY wY w xY w# 1 sw Y   xY wr   )
r`  rc  popleft
IndexErrorrd  re  r|   StopIterationr	   r   )r    r  rq   r[  r;   s        r   r?  zIMapIterator.nextU  s    ZZ 	11{{**,	1  L!  1;;$,,.!%DJ'T1

(1;;..0D! 1{{dll2%)
+5&D0	1 1	1 	1s?   C$<AC!B#"C!#8CC!C$ C!!C$$C-c                    | j                   5  | j                  |k(  r| j                  j                  |       | xj                  dz  c_        | j                  | j                  v rn| j                  j                  | j                        }| j                  j                  |       | xj                  dz  c_        | j                  | j                  v rn| j                   j                          n|| j                  |<   | j                  | j                  k(  r| j                  | j                  = d | _
        d d d        y # 1 sw Y   y xY wNr   )r`  rd  rc  r   rf  popnotifyre  r   r   r|   rO  s      r   r  zIMapIterator._setm  s    ZZ 	"{{a""3'q kkT^^3..,,T[[9CKK&&s+KK1$K kkT^^3 

!!#$'q!{{dll*KK		*!
	" 	" 	"s   CD=A!D==Ec                     | j                   5  || _        | j                  | j                  k(  r8| j                   j                          | j                  | j
                  = d | _        d d d        y # 1 sw Y   y xY wr   )r`  re  rd  rp  r   r   r|   )r    rX  s     r   r   zIMapIterator._set_length~  s\    ZZ 	"!DL{{dll*

!!#KK		*!
	" 	" 	"s   AA//A8r   )	r%   r&   r'   r!   rh  r?  __next__r  r   r(   r   r   r   r   E  s"    	&, H"""r   r   c                       e Zd Zd Zy)r   c                 H   | j                   5  | j                  j                  |       | xj                  dz  c_        | j                   j	                          | j                  | j
                  k(  r| j                  | j                  = d | _        d d d        y # 1 sw Y   y xY wrn  )	r`  rc  r   rd  rp  re  r   r   r|   rO  s      r   r  zIMapUnorderedIterator._set  su    ZZ 	"KKs#KK1KJJ{{dll*KK		*!
	" 	" 	"s   BBB!N)r%   r&   r'   r  r(   r   r   r   r     s    "r   r   c                   Z    e Zd ZdZed        Zd
dZd Zd Zed        Z	ed        Z
d	 Zy)r   Fc                     ddl m}  ||i |S )Nr   rt   )dummyru   )rv   r   rc   ru   s       r   ru   zThreadPool.Process  s    "%%%r   Nc                 4    t         j                  | |||       y 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                  | _        y r   )r   r   r   r   rM   r   rN   r   r#   s    r   r   zThreadPool._setup_queues  sD    ))+**,--++..,,r   c                 0    | 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                     	 	 | j                  d       # t        j                  $ r Y nw xY wt        |      D ]  }| j	                  d         y )NF)block)rN   r   Emptyr   rM   )rX   r*  r  ra   s       r   r+  zThreadPool._help_stuff_finish  sR    	%( {{ 		t 	AKK	s    ,,c                 .    t        j                  |       y r   )r(  r)  )r    r   r  r  s       r   r  zThreadPool._wait_for_updates  s    

7r   )NNr(   )r%   r&   r'   r   r7  ru   r!   r   r   r   r+  r  r(   r   r   r   r     sU    O& &>-/    r   )Nr(   NF))__all__ra  r   r   r   r   r(  r-   rP  r8  r,   r   r   r   
connectionr	   r
   r   r   r   countr@  r   r   rU   r   r*   r3   r8   rg   rV   dictrk   objectr   r   AsyncResultr   r   r   r   r(   r   r   <module>r     s   <
    	        '  	 ioo5i 0 0< <" GK+<Z$ $.v6 vx)8& )8V %" %"V?"6 ?"J	"L 	"$ $r   