a bh @sdZdZddlmZddlZddlZddlZddlZddlZddl Z e Z da e ZddZeeGdd d eZd d ZGd d d ejZGdddejZdS)zImplements ThreadPoolExecutor.z"Brian Quinlan (brian@sweetapp.com))_baseNFcCsftdaWdn1s0Ytt}|D]\}}|dq8|D]\}}|qPdSNT)_global_shutdown_lock _shutdownlist_threads_queuesitemsputjoin)rtqr :/opt/imh-python/lib/python3.9/concurrent/futures/thread.py _python_exits"    rc@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 rrun/s  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_worker?s8      r3c@seZdZdZdS)BrokenThreadPoolzR Raised when a worker thread in a ThreadPoolExecutor failed initializing. N)rr r!__doc__r r r rr4jsr4c@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 Semaphorer,set_threads_brokenrLock_shutdown_lock_counter_thread_name_prefix _initializer _initargs)r max_workersZthread_name_prefixr/r0r r rrus$     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) rKrrIr4r RuntimeErrorrFuturerrDr _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)r,acquirerDlenrHrArMrEThreadr3weakrefrefrNrOstartaddr)rrYZ num_threadsZ thread_namer r r rrSs$     z'ThreadPoolExecutor._adjust_thread_countc Cst|jZd|_z|j}Wntjy4YqRYn0|dur|jt|jqWdn1sf0YdS)NzBA thread initializer failed, the thread pool is not usable anymore) rKrIrD get_nowaitrBEmptyrrr4)rr2r r rr*s z&ThreadPoolExecutor._initializer_failedTF)cancel_futuresc Cs|jbd|_|rNz|j}Wntjy8YqNYn0|dur|jq|jdWdn1sn0Y|r|j D] }| qdSr) rKrrDrcrBrdrcancelr rHr )rwaitrer2r r r rshutdowns  * zThreadPoolExecutor.shutdown)Nr7Nr )T)rr r! itertoolscount__next__rLrrVrExecutorr5rSr*rhr r r rr6ps  &  r6)r5 __author__concurrent.futuresrrirBrEr#r_r<WeakKeyDictionaryrrrJrr_register_atexitobjectrr3BrokenExecutorr4rlr6r r r rs"  +