
    0Fie                        U d Z ddlmZ d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  e	j                    Zded<   dad	 Z ej        e            G d
 d          Zd Z G d dej                  ZdS )zImplements ThreadPoolExecutor.    )annotationsz"Brian Quinlan (brian@sweetapp.com)N)_basez8weakref.WeakKeyDictionary[threading.Thread, queue.Queue]_threads_queuesFc                     da t          t                                                    } | D ]\  }}|                    d            | D ]\  }}|                                 d S NT)	_shutdownlistr   itemsputjoin)r
   tqs      Flib/python3.11/site-packages/distributed/_concurrent_futures_thread.py_python_exitr   (   sp    I&&(())E  1	d  1	     c                      e Zd Zd Zd ZdS )	_WorkItemc                >    || _         || _        || _        || _        d S N)futurefnargskwargs)selfr   r   r   r   s        r   __init__z_WorkItem.__init__6   s"    	r   c                   | j                                         sd S 	  | j        | j        i | j        }| j                             |           d S # t          $ r%}| j                             |           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es      r   runz_WorkItem.run<   s    {7799 	F	+TWdi74;77F K""6*****  	) 	) 	)K%%a(((((((((	)s   A 
A?A::A?N)__name__
__module____qualname__r   r#    r   r   r   r   5   s2          	+ 	+ 	+ 	+ 	+r   r   c                ,   	 	 |                     d          }||                                 ~/ |             }t          s	||j        r|                    d            d S ~a# t          $ r% t
          j                            dd           Y d S w xY w)NT)blockzException in worker)exc_info)getr#   r   r   r   r   LOGGERcritical)executor_reference
work_queue	work_itemexecutors       r   _workerr2   H   s    D	"T22I$))++H
  H,0B,t$$$!	"  D D D3dCCCCCCDs   AA$ "A$ $+BBc                      e Zd Z ej                    ZddZd Zej	        j        j
        e_
        d Zd	dZej	        j        j
        e_
        dS )
ThreadPoolExecutorN c                B   |t          j                    pddz  }|dk    rt          d          || _        t	          j                    | _        t                      | _        d| _	        t          j                    | _        |pdt          | j                  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.
        N      r   z"max_workers must be greater than 0FzThreadPoolExecutor-%d)os	cpu_count
ValueError_max_workersqueueQueue_work_queueset_threadsr   	threadingLock_shutdown_locknext_counter_thread_name_prefix)r   max_workersthread_name_prefixs      r   r   zThreadPoolExecutor.__init__c   s      <>>.Q!3K!ABBB' ;=='n..#5 $
#d4=&9&99 	   r   c                   | j         5  | j        rt          d          t          j                    }t          ||||          }| j                            |           |                                  |cd d d            S # 1 swxY w Y   d S )Nz*cannot schedule new futures after shutdown)	rD   r   RuntimeErrorr   Futurer   r?   r   _adjust_thread_count)r   r   r   r   fws         r   submitzThreadPoolExecutor.submit{   s      		 		~ Q"#OPPPA!Rv..A  ###%%'''		 		 		 		 		 		 		 		 		 		 		 		 		 		 		 		 		 		s   A+B  BBc                l   | j         fd}t          | j                  }|| j        k     rd| j        p| |fz  }t          j        |t          t          j	        | |          | j         f          }d|_
        |                                 | j                            |           | j         t          |<   d S d S )Nc                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   T)r?   lenrA   r<   rG   rB   Threadr2   weakrefrefdaemonstartaddr   )r   rT   num_threadsthread_namer   s        r   rM   z'ThreadPoolExecutor._adjust_thread_count   s     !, 	 	 	 	
 $-((***!T%=%E{$SSK  k$
33T5EF  A
 AHGGIIIMa   !%!1OA +*r   Tc                    | j         5  d| _        | j                            d            d d d            n# 1 swxY w Y   |r| j        D ]}|                                 d S d S r   )rD   r   r?   r   rA   r   )r   waitr   s      r   shutdownzThreadPoolExecutor.shutdown   s      	' 	'!DN  &&&	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	'  	]  	 	 s   "6::)Nr5   )T)r$   r%   r&   	itertoolscountrF   r   rP   r   Executor__doc__rM   rb   r'   r   r   r4   r4   _   s        y  H
 
 
 
0
 
 
 ^*2FN2 2 2*    ~.6Hr   r4   )rf   
__future__r   
__author__atexitrc   r9   r=   rB   rY   concurrent.futuresr   WeakKeyDictionaryr   __annotations__r   r   registerr   r2   re   r4   r'   r   r   <module>rn      sB   % $ $ " " " " " "1
      				       $ $ $ $ $ $$ G          	      + + + + + + + +&D D D.G7 G7 G7 G7 G7 G7 G7 G7 G7 G7r   