o *gC"@sdZdZddlmZddlZddlZddlZddlZddlZddl Z e Z da e ZddZeeee drEe jejejejd Gd d d eZd d ZGdddejZGdddejZdS)zImplements ThreadPoolExecutor.z"Brian Quinlan (brian@sweetapp.com))_baseNFcCsft daWdn1swYtt}|D] \}}|dq|D]\}}|q(dSNT)_global_shutdown_lock _shutdownlist_threads_queuesitemsputjoin)rtqr 0/usr/lib/python3.10/concurrent/futures/thread.py _python_exits     rregister_at_fork)beforeafter_in_childafter_in_parentc@s&eZdZddZddZeejZdS) _WorkItemcCs||_||_||_||_dSN)futurefnargskwargs)selfrrrrr r r__init__/s z_WorkItem.__init__c Csn|jsdSz |j|ji|j}Wnty.}z|j|d}WYd}~dSd}~ww|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|dur(z||Wnty'tjjddd|}|dur$|YdSwz; |jdd}|durG|~|}|durE|j~q)|}t sS|dusS|j rb|durZd|_ | dWdS~q*tyutjjdddYdSw)NzException in initializer:T)exc_info)blockzException in worker) rrLOGGERcritical_initializer_failedgetr"_idle_semaphorereleaserr )executor_reference work_queue initializerinitargsexecutor work_itemr r r_workerEs@      r8c@seZdZdZdS)BrokenThreadPoolzR Raised when a worker thread in a ThreadPoolExecutor failed initializing. N)r#r$r%__doc__r r r rr9psr9c@sdeZdZejZ  dddZddZe j jj e_ dd Z d d Z dd dddZe j jj e_ dS)ThreadPoolExecutorNr cCs|durtdtp dd}|dkrtd|dur#t|s#td||_t|_ t d|_ t |_d|_d|_t |_|pGd ||_||_||_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_workersthread_name_prefixr4r5r r rr{s$      zThreadPoolExecutor.__init__c Os|jNt;|jrt|j|jrtdtrtdt}t||||}|j || |WdWdS1sDwYWddS1sTwYdS)Nz*cannot schedule new futures after shutdownz6cannot schedule new futures after interpreter shutdown) rPrrNr9r RuntimeErrorrFuturerrIr _adjust_thread_count)rrrrfwr r rsubmits  RzThreadPoolExecutor.submitcCs|jjddr dS|jfdd}t|j}||jkrGd|jp||f}tj|t t |||j|j |j fd}||j||jt|<dSdS)Nr)timeoutcSs|ddSr)r )_r r r r weakref_cbsz;ThreadPoolExecutor._adjust_thread_count..weakref_cbz%s_%d)nametargetr)r0acquirerIlenrMrFrRrJThreadr8weakrefrefrSrTstartaddr)rr_ num_threads thread_namer r r rrYs&     z'ThreadPoolExecutor._adjust_thread_countc Cst|j-d|_ z|j}Wn tjyYnw|dur'|jt|jqWddS1s3wYdS)NzBA thread initializer failed, the thread pool is not usable anymore) rPrNrI get_nowaitrGEmptyrrr9)rr7r r rr.s"z&ThreadPoolExecutor._initializer_failedTF)cancel_futuresc Cs|j0d|_|r& z|j}Wn tjyYn w|dur%|jq |jdWdn1s6wY|rG|j D]}| q@dSdSr) rPrrIrkrGrlrcancelr rMr )rwaitrmr7r r r rshutdowns&    zThreadPoolExecutor.shutdown)Nr<Nr )T)r#r$r% itertoolscount__next__rQrr\rExecutorr:rYr.rpr r r rr;vs  &  r;)r: __author__concurrent.futuresrrqrGrJr'rerAWeakKeyDictionaryrrrOrr_register_atexithasattrrrb_at_fork_reinitr1objectrr8BrokenExecutorr9rtr;r r r rs.   +