U e5d@"@sdZdZddlZddlmZddlZddlZddlZddlZddl Z e Z da ddZ ee Gdd d eZd d ZGd d d ejZGdddejZdS)zImplements ThreadPoolExecutor.z"Brian Quinlan (brian@sweetapp.com)N)_baseFcCsBdatt}|D]\}}|dq|D]\}}|q,dSNT) _shutdownlist_threads_queuesitemsputjoin)rtqr 1/usr/lib64/python3.8/concurrent/futures/thread.py _python_exit!s     rc@seZdZddZddZdS) _WorkItemcCs||_||_||_||_dSN)futurefnargskwargs)selfrrrrr r r __init__.sz_WorkItem.__init__c Csf|jsdSz|j|j|j}Wn2tk rT}z|j|d}W5d}~XYnX|j|dSr)rZset_running_or_notify_cancelrrr BaseException set_exceptionZ set_result)rresultexcr r r run4s  z _WorkItem.runN)__name__ __module__ __qualname__rrr r r r r-srcCs|dk rRz ||Wn<tk rPtjjddd|}|dk rJ|YdSXzx|jdd}|dk r|~|}|dk r|j~qT|}t s|dks|j r|dk rd|_ | dWdS~qTWn$tk rtjjdddYnXdS)NzException in initializer:T)exc_info)blockzException in worker) rrZLOGGERZcritical_initializer_failedgetr_idle_semaphorereleaserr)Zexecutor_referenceZ work_queue initializerinitargsZexecutor work_itemr r r _workerBs8    r(c@seZdZdZdS)BrokenThreadPoolzR Raised when a worker thread in a ThreadPoolExecutor failed initializing. N)rrr__doc__r r r r r)msr)c@sfeZdZejZdddZddZe j jj e_ e j jj e_ dd Z d d Zdd dZe j jj e_ dS)ThreadPoolExecutorNr cCs|dkrtdtpdd}|dkr.td|dk rFt|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_workersqueueZ SimpleQueue _work_queue threadingZ Semaphorer#set_threads_brokenrZLock_shutdown_lock_counter_thread_name_prefix _initializer _initargs)rZ max_workersZthread_name_prefixr%r&r r r rxs$     zThreadPoolExecutor.__init__c Ost|dkr|^}}}nV|s&tdnHd|krZ|d}|^}}ddl}|jdtddntdt|d|jf|jrt|j|j rt d t rt d t }t ||||}|j|||W5QRSQRXdS) NzDdescriptor 'submit' of 'ThreadPoolExecutor' object needs an argumentrrz.Passing 'fn' as keyword argument is deprecated) stacklevelz6submit expected at least 1 positional argument, got %dr.z*cannot schedule new futures after shutdownz6cannot schedule new futures after interpreter shutdown)lenr5popwarningswarnDeprecationWarningr=r<r)r RuntimeErrorrZFuturerr8r_adjust_thread_count)rrrrrFfwr r r submits6       zThreadPoolExecutor.submitcCs|jjddrdS|jfdd}t|j}||jkrd|jp>||f}tj|t t |||j|j |j fd}d|_||j||jt|<dS)Nr)ZtimeoutcSs|ddSr)r)_r r r r weakref_cbsz;ThreadPoolExecutor._adjust_thread_count..weakref_cbz%s_%d)nametargetrT)r#acquirer8rDr;r6r?r9ZThreadr(weakrefrefr@rAZdaemonstartaddr)rrOZ num_threadsZ thread_namer r r r rJs&     z'ThreadPoolExecutor._adjust_thread_countc Csb|jRd|_z|j}Wntjk r6YqTYnX|dk r|jt|jqW5QRXdS)NzBA thread initializer failed, the thread pool is not usable anymore) r=r<r8Z get_nowaitr7ZEmptyrrr))rr'r r r r!s z&ThreadPoolExecutor._initializer_failedTc Cs@|jd|_|jdW5QRX|r<|jD] }|q.dSr)r=rr8rr;r )rwaitr r r r shutdowns  zThreadPoolExecutor.shutdown)Nr,Nr )T)rrr itertoolscount__next__r>rrMrExecutor__text_signature__r*rJr!rXr r r r r+ss  &   r+)r* __author__atexitZconcurrent.futuresrrYr7r9rSr1WeakKeyDictionaryrrrregisterobjectrr(ZBrokenExecutorr)r\r+r r r r s   +