a DOgC"@sdZdZddlmZddlZddlZddlZddlZddlZddl Z e Z da e ZddZeeee dre jejejejd Gd d d eZd d ZGdddejZGdddejZdS)zImplements ThreadPoolExecutor.z"Brian Quinlan (brian@sweetapp.com))_baseNFcCsftdaWdn1s0Ytt}|D]\}}|dq8|D]\}}|qPdSNT)_global_shutdown_lock _shutdownlist_threads_queuesitemsputjoin)rtqr 1/usr/lib64/python3.9/concurrent/futures/thread.py _python_exits"    rregister_at_fork)beforeafter_in_childafter_in_parentc@s&eZdZddZddZeejZdS) _WorkItemcCs||_||_||_||_dSN)futurefnargskwargs)selfrrrrr r r__init__/sz_WorkItem.__init__c Csl|jsdSz|j|ji|j}Wn4tyZ}z|j|d}WYd}~nd}~00|j|dSr)rset_running_or_notify_cancelrrr BaseException set_exception set_result)rresultexcr r rrun5s  z _WorkItem.runN) __name__ __module__ __qualname__rr" classmethodtypes GenericAlias__class_getitem__r r r rr.s rcCs|durPz ||Wn:tyNtjjddd|}|durH|YdS0zx|jdd}|dur|~|}|dur|j~qR|}t s|dus|j r|durd|_ | dWdS~qRWn"tytjjdddYn0dS)NzException in initializer:T)exc_info)blockzException in worker) rrLOGGERcritical_initializer_failedgetr"_idle_semaphorereleaserr )executor_referenceZ work_queue initializerinitargsexecutor work_itemr r r_workerEs8      r7c@seZdZdZdS)BrokenThreadPoolzR Raised when a worker thread in a ThreadPoolExecutor failed initializing. N)r#r$r%__doc__r r r rr8psr8c@s`eZdZejZdddZddZe j jj e_ dd Z d d Z dd dddZe j jj e_ dS)ThreadPoolExecutorNr cCs|durtdtpdd}|dkr.td|durFt|sFtd||_t|_ t d|_ t |_d|_d|_t |_|pd ||_||_||_dS) aInitializes 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 rz"max_workers must be greater than 0zinitializer must be a callableFzThreadPoolExecutor-%d)minos cpu_count ValueErrorcallable TypeError _max_workersqueue SimpleQueue _work_queue threading Semaphorer0set_threads_brokenrLock_shutdown_lock_counter_thread_name_prefix _initializer _initargs)r max_workersZthread_name_prefixr3r4r r rr{s$     zThreadPoolExecutor.__init__c Os|jtv|jrt|j|jr,tdtr8tdt}t||||}|j || |WdWdS1s0YWdn1s0YdS)Nz*cannot schedule new futures after shutdownz6cannot schedule new futures after interpreter shutdown) rOrrMr8r RuntimeErrorrFuturerrHr _adjust_thread_count)rrrrfwr r rsubmits  zThreadPoolExecutor.submitcCs|jjddrdS|jfdd}t|j}||jkrd|jp>||f}tj|t t |||j|j |j fd}||j||jt|<dS)Nr)timeoutcSs|ddSr)r )_r r r r weakref_cbsz;ThreadPoolExecutor._adjust_thread_count..weakref_cbz%s_%d)nametargetr)r0acquirerHlenrLrErQrIThreadr7weakrefrefrRrSstartaddr)rr]Z num_threadsZ thread_namer r r rrWs$     z'ThreadPoolExecutor._adjust_thread_countc Cst|jZd|_z|j}Wntjy4YqRYn0|dur|jt|jqWdn1sf0YdS)NzBA thread initializer failed, the thread pool is not usable anymore) rOrMrH get_nowaitrFEmptyrrr8)rr6r r rr.s z&ThreadPoolExecutor._initializer_failedTF)cancel_futuresc Cs|jbd|_|rNz|j}Wntjy8YqNYn0|dur|jq|jdWdn1sn0Y|r|j D] }| qdSr) rOrrHrgrFrhrcancelr rLr )rwaitrir6r r r rshutdowns  * zThreadPoolExecutor.shutdown)Nr;Nr )T)r#r$r% itertoolscount__next__rPrrZrExecutorr9rWr.rlr r r rr:vs  &  r:)r9 __author__concurrent.futuresrrmrFrIr'rcr@WeakKeyDictionaryrrrNrr_register_atexithasattrrr`_at_fork_reinitr1objectrr7BrokenExecutorr8rpr:r r r rs,   +