
    Rh"                        d Z dZddlm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	 ej                         Zda ej                         Zd Z ej                   e        ee	d      r3 e	j$                  ej&                  ej(                  ej*                          G d	 d
      Zd Z G d dej0                        Z G d dej4                        Zy)zImplements ThreadPoolExecutor.z"Brian Quinlan (brian@sweetapp.com)    )_baseNFc                      t         5  dad d d        t        t        j	                               } | D ]  \  }}|j                  d         | D ]  \  }}|j                           y # 1 sw Y   \xY wNT)_global_shutdown_lock	_shutdownlist_threads_queuesitemsputjoin)r
   tqs      0/usr/lib/python3.12/concurrent/futures/thread.py_python_exitr      sl    	 	&&()E 1	d 1	 s   A%%A.register_at_fork)beforeafter_in_childafter_in_parentc                   <    e Zd Zd Zd Z eej                        Zy)	_WorkItemc                 <    || _         || _        || _        || _        y N)futurefnargskwargs)selfr   r   r   r   s        r   __init__z_WorkItem.__init__/   s    	    c                 $   | j                   j                         sy 	  | j                  | j                  i | j                  }| j                   j                  |       y # t        $ r'}| j                   j                  |       d } Y d }~y d }~ww xY wr   )r   set_running_or_notify_cancelr   r   r   
set_resultBaseExceptionset_exception)r   resultexcs      r   runz_WorkItem.run5   st    {{779	+TWWdii74;;7F KK""6*  	KK%%c*D	s   &A 	B(B

BN)	__name__
__module____qualname__r   r'   classmethodtypesGenericAlias__class_getitem__ r   r   r   r   .   s    + $E$6$67r   r   c                 `   |	  ||  	 	 	 |j                         }||j                          ~& |        }t        s||j                  r|d|_        |j                  d        y ~]# t         $ r= t        j                  j                  dd        |        }||j	                          Y y w xY w# t        j                  $ r9  |        }||j                  j                          ~|j                  d      }Y w xY w# t         $ r$ t        j                  j                  dd       Y y w xY w)NzException in initializer:T)exc_info)blockzException in worker)r#   r   LOGGERcritical_initializer_failed
get_nowaitqueueEmpty_idle_semaphorereleasegetr'   r   r   )executor_reference
work_queueinitializerinitargsexecutor	work_items         r   _workerrB   E   s@   	"!D7&113	 $)+H
 H,0B0B ')-H&t$=   	LL!!"=!M)+H#,,.	 ;; 7-/',,446&NNN6	78  D3dCDsM   A( D  B1 A	D  &D  (AB.-B.1A	C=:D  <C==D   *D-,D-c                       e Zd ZdZy)BrokenThreadPoolzR
    Raised when a worker thread in a ThreadPoolExecutor failed initializing.
    N)r(   r)   r*   __doc__r/   r   r   rD   rD   s   s    r   rD   c                      e Zd Z ej                         j
                  Z	 	 d	dZd Ze	j                  j                  j                  e_        d Zd Zd
dddZe	j                  j                  j                  e_        y)ThreadPoolExecutorNc                    |%t        dt        j                         xs ddz         }|dk  rt        d      |t	        |      st        d      || _        t        j                         | _	        t        j                  d      | _        t               | _        d| _        d| _        t        j"                         | _        |xs d	| j'                         z  | _        || _        || _        y)
a  Initializes a new ThreadPoolExecutor instance.

        Args:
            max_workers: The maximum number of threads that can be used to
                execute the given calls.
            thread_name_prefix: An optional name prefix to give our threads.
            initializer: A callable used to initialize worker threads.
            initargs: A tuple of arguments to pass to the initializer.
        N          r   z"max_workers must be greater than 0zinitializer must be a callableFzThreadPoolExecutor-%d)minos	cpu_count
ValueErrorcallable	TypeError_max_workersr7   SimpleQueue_work_queue	threading	Semaphorer9   set_threads_brokenr   Lock_shutdown_lock_counter_thread_name_prefix_initializer	_initargs)r   max_workersthread_name_prefixr>   r?   s        r   r   zThreadPoolExecutor.__init__~   s      b2<<>#6Q!";<K!ABB"8K+@<==' ,,.(2215'nn.$6 %P%<t}}%N 	 '!r   c                   | j                   5  t        5  | j                  rt        | j                        | j                  rt        d      t        rt        d      t        j                         }t        ||||      }| j                  j                  |       | j                          |cd d d        cd d d        S # 1 sw Y   nxY wd d d        y # 1 sw Y   y xY w)Nz*cannot schedule new futures after shutdownz6cannot schedule new futures after interpreter shutdown)r[   r   rY   rD   r   RuntimeErrorr   Futurer   rT   r   _adjust_thread_count)r   r   r   r   fws         r   submitzThreadPoolExecutor.submit   s       	"7 	||&t||44~~"#OPP" $: ; ; A!Rv.A  #%%'	 	 	 	 	 	 	s#   CBB?,	C?C	CCc                    | j                   j                  d      ry | j                  fd}t        | j                        }|| j
                  k  rd| j                  xs | |fz  }t        j                  |t        t        j                  | |      | j                  | j                  | j                  f      }|j                          | j                  j                  |       | j                  t         |<   y y )Nr   )timeoutc                 &    |j                  d        y r   )r   )_r   s     r   
weakref_cbz;ThreadPoolExecutor._adjust_thread_count.<locals>.weakref_cb   s    EE$Kr   z%s_%d)nametargetr   )r9   acquirerT   lenrX   rR   r]   rU   ThreadrB   weakrefrefr^   r_   startaddr	   )r   rm   num_threadsthread_namer   s        r   re   z'ThreadPoolExecutor._adjust_thread_count   s    '''2 !,, 	 $--(***!T%=%=%E%0%2 2K  k''.{{4'D'+'7'7'+'8'8'+~~'78A
 GGIMMa !%!1!1OA +r   c                 "   | j                   5  d| _        	 	 | j                  j                         }|.|j                  j                  t        | j                               L# t        j
                  $ r Y nw xY w	 d d d        y # 1 sw Y   y xY w)NzBA thread initializer failed, the thread pool is not usable anymore)	r[   rY   rT   r6   r7   r8   r   r$   rD   )r   rA   s     r   r5   z&ThreadPoolExecutor._initializer_failed   s       
	S4DL  $ 0 0 ; ; =I ($$223CDLL3QR  {{ 
	S 
	S 
	Ss-   	BA"1B"A85B7A88BBF)cancel_futuresc                z   | j                   5  d| _        |r9	 	 | j                  j                         }||j                  j                          8| j                  j                  d        d d d        |r"| j                  D ]  }|j                           y y # t        j
                  $ r Y ]w xY w# 1 sw Y   GxY wr   )r[   r   rT   r6   r7   r8   r   cancelr   rX   r   )r   waitrz   rA   r   s        r   shutdownzThreadPoolExecutor.shutdown   s       	'!DN $($4$4$?$?$A	 !,!((//1    &	'  ]]   !;; 	' 	's-   B1B8B1B.+B1-B..B11B:)N Nr/   )T)r(   r)   r*   	itertoolscount__next__r\   r   rh   r   ExecutorrE   re   r5   r~   r/   r   r   rG   rG   y   ss     y ))H<>,.$"L" ^^**22FN2.SE ( ~~..66Hr   rG   )rE   
__author__concurrent.futuresr   r   r7   rU   r,   rs   rM   WeakKeyDictionaryr	   r   rZ   r   r   _register_atexithasattrr   rp   _at_fork_reinitr:   r   rB   BrokenExecutorrD   r   rG   r/   r   r   <module>r      s    %1
 $      	 ,'++-	 '	(  	  < ( 2!"B4<<'<'L'L(=(E(EG
8 8.+D\u++ v7 v7r   