
    dC"                     z   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          r" e	j        ej        ej        ej                    G d	 d
e          Zd Z G d dej                  Z G d dej                  ZdS )zImplements ThreadPoolExecutor.z"Brian Quinlan (brian@sweetapp.com)    )_baseNFc                     t           5  dad d d            n# 1 swxY w Y   t          t                                                    } | D ]\  }}|                    d            | D ]\  }}|                                 d S NT)_global_shutdown_lock	_shutdownlist_threads_queuesitemsputjoin)r
   tqs      +lib/python3.11/concurrent/futures/thread.py_python_exitr      s    	  	              &&(())E  1	d  1	 s   register_at_fork)beforeafter_in_childafter_in_parentc                   :    e Zd Zd Zd Z eej                  ZdS )	_WorkItemc                 >    || _         || _        || _        || _        d S N)futurefnargskwargs)selfr   r   r   r   s        r   __init__z_WorkItem.__init__/   s"    	    c                    | j                                         sd S 	  | j        | j        i | j        }| j                             |           d S # t          $ r'}| j                             |           d } Y d }~d S 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   s    {7799 	F	+TWdi74;77F K""6*****  	 	 	K%%c***DDDDDDD	s   A 
BA<<BN)	__name__
__module____qualname__r   r'   classmethodtypesGenericAlias__class_getitem__ r   r   r   r   .   sC          + + + $E$677r   r   c                 @   |Y	  ||  nR# t           $ rE t          j                            dd            |             }||                                 Y d S w xY w	 	 |                    d          }|<|                                 ~ |             }||j                                         ~U |             }t          s	||j	        r |d|_	        |
                    d            d S ~# t           $ r% t          j                            dd           Y d S w xY w)NzException in initializer:T)exc_info)blockzException in worker)r#   r   LOGGERcritical_initializer_failedgetr'   _idle_semaphorereleaser   r   )executor_reference
work_queueinitializerinitargsexecutor	work_items         r   _workerr?   E   s   	K""" 	 	 	L!!"=!MMM))++H#,,...FF	D	"T22I$ .-//',44666))++H
  H,0B, ')-H&t$$$7	8  D D D3dCCCCCCDs)   
 AAABC. ,C. .+DDc                       e Zd ZdZdS )BrokenThreadPoolzR
    Raised when a worker thread in a ThreadPoolExecutor failed initializing.
    N)r(   r)   r*   __doc__r/   r   r   rA   rA   p   s           r   rA   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_        dS )ThreadPoolExecutorN r/   c                    |&t          dt          j                    pddz             }|dk    rt          d          |t	          |          st          d          || _        t          j                    | _	        t          j        d          | _        t                      | _        d| _        d| _        t          j                    | _        |pd	|                                 z  | _        || _        || _        dS )
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_workersqueueSimpleQueue_work_queue	threading	Semaphorer7   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B"8K+@+@"<===' ,..(2155'n..$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	        
                    |           |                                  |cd d d            cd d d            S # 1 swxY w Y   d d d            d S # 1 swxY w Y   d S )Nz*cannot schedule new futures after shutdownz6cannot schedule new futures after interpreter shutdown)rZ   r   rX   rA   r   RuntimeErrorr   Futurer   rS   r   _adjust_thread_count)r   r   r   r   fws         r   submitzThreadPoolExecutor.submit   s     	 	"7 	 	| 5&t|444~ Q"#OPPP ;" $: ; ; ; A!Rv..A  ###%%'''	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	s5   CBC,CC			CC		CC!$C!c                    | j                             d          rd S | j        fd}t          | j                  }|| j        k     rd| j        p| |fz  }t          j        |t          t          j        | |          | j        | j        | j        f          }|                                 | j                            |           | j        t           |<   d S d S )Nr   )timeoutc                 0    |                     d            d S r   )r   )_r   s     r   
weakref_cbz;ThreadPoolExecutor._adjust_thread_count.<locals>.weakref_cb   s    EE$KKKKKr   z%s_%d)nametargetr   )r7   acquirerS   lenrW   rP   r\   rT   Threadr?   weakrefrefr]   r^   startaddr	   )r   rl   num_threadsthread_namer   s        r   rd   z'ThreadPoolExecutor._adjust_thread_count   s    '''22 	F !, 	 	 	 	 $-((***!T%=%E%0%2 2K k''.{4'D'D'+'7'+'8'+~'78 8 8A
 GGIIIMa   !%!1OA +*r   c                    | j         5  d| _        	 	 | j                                        }n# t          j        $ r Y n3w xY w|,|j                            t          | j                             _	 d d d            d S # 1 swxY w Y   d S )NzBA thread initializer failed, the thread pool is not usable anymore)	rZ   rX   rS   
get_nowaitrQ   Emptyr   r$   rA   )r   r>   s     r   r5   z&ThreadPoolExecutor._initializer_failed   s     
	S 
	S4DLS $ 0 ; ; = =II{   E($223CDL3Q3QRRRS 
	S 
	S 
	S 
	S 
	S 
	S 
	S 
	S 
	S 
	S 
	S 
	S 
	S 
	S 
	S 
	S 
	S 
	Ss,   	A>,A>>A>>2A>>BBTF)cancel_futuresc                h   | j         5  d| _        |rM	 	 | j                                        }n# t          j        $ r Y n w xY w||j                                         L| j                            d            d d d            n# 1 swxY w Y   |r| j	        D ]}|
                                 d S d S r   )rZ   r   rS   ry   rQ   rz   r   cancelr   rW   r   )r   waitr{   r>   r   s        r   shutdownzThreadPoolExecutor.shutdown   s(     	' 	'!DN 	22$($4$?$?$A$A		 ;    ,!(//1112   &&&	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	'   	]  	 	 s/   B.BA BA  9BB	B	)NrE   Nr/   )T)r(   r)   r*   	itertoolscount__next__r[   r   rg   r   ExecutorrB   rd   r5   r   r/   r   r   rD   rD   v   s         y  )H<>,.$" $" $" $"L  " ^*2FN2 2 2.S S SE     ( ~.6Hr   rD   )rB   
__author__concurrent.futuresr   r   rQ   rT   r,   rr   rK   WeakKeyDictionaryr	   r   rY   r   r   _register_atexithasattrr   ro   _at_fork_reinitr8   objectr   r?   BrokenExecutorrA   r   rD   r/   r   r   <module>r      s   % $1
 $ $ $ $ $ $            				 ,'+--	 '	((    	 < ( ( ( 72!"" GB4<'<'L(=(EG G G G
8 8 8 8 8 8 8 8.(D (D (DV    u+   v7 v7 v7 v7 v7 v7 v7 v7 v7 v7r   