Ϫf*dZddlmZddlmZmZddlmZmZm Z m Z m Z ddlm Z m Z mZddlmZddlmZmZddlmZdd lmZdd lmZe d Ze d ZGd de ZGddeZeZ GddZ!y)z twisted.python.threadpool: a pool of threads to which we dispatch tasks. In most cases you can just use C{reactor.callInThread} and friends instead of creating a thread pool directly. ) annotations)Threadcurrent_thread)AnyCallableListOptionalTypeVar) ParamSpecProtocol TypedDict)pool)contextlog) deprecated)Failure)Version_P_RceZdZddZy)_SupportsQsizecyNselfs ;/usr/lib/python3/dist-packages/twisted/python/threadpool.pyqsizez_SupportsQsize.qsizes Nreturnint__name__ __module__ __qualname__rrrrrrs rrc"eZdZUded<ded<y)_Stater"minmaxN)r$r%r&__annotations__rrrr(r("s H Hrr(c beZdZdZdZdZdZdZdZe Z e e e dddd d eZe eZ d dd Zedd ZeddZeddZed dZeZd!dZd!dZd"dZd!dZd#dZd$dZ d%dZ d&dZd!dZ d' d(dZ!d!dZ"y)) ThreadPoola This class (hopefully) generalizes the functionality of a pool of threads to which work can be dispatched. L{callInThread} and L{stop} should only be called from a single thread. @ivar started: Whether or not the thread pool is currently running. @type started: L{bool} @ivar threads: List of workers currently running in this thread pool. @type threads: L{list} @ivar _pool: A hook for testing. @type _pool: callable compatible with L{_pool} FNTwistedrzthreading.current_thread)version replacementc|dk\sJd||ksJd|_|_|_g_dfd }dfd }j ||_y) ac Create a new threadpool. @param minthreads: minimum number of threads in the pool @type minthreads: L{int} @param maxthreads: maximum number of threads in the pool @type maxthreads: L{int} @param name: The name to give this threadpool; visible in log messages. @type name: native L{str} rminimum is negativeminimum is greater than maximumcj|dji|}jj||S)Nname) threadFactory _generateNamethreadsappend)akwthreadrs rtrackingThreadFactoryz2ThreadPool.__init__..trackingThreadFactory`sJ'T''++-13F LL   'Mrc6jsyjS)Nr)startedr*rsr currentLimitz)ThreadPool.__init__..currentLimitgs<<88OrN)r>rr?rr!rr )r)r*r9r<_pool_team)r minthreads maxthreadsr9rArDs` r__init__zThreadPool.__init__JsfQ5 55Z'J)JJ' %'    ZZ .CD rch|jj}|j|jzS)a For legacy compatibility purposes, return a total number of workers. @return: the current number of workers, both idle and busy (but not those that have been quit by L{ThreadPool.adjustPoolsize}) @rtype: L{int} )rF statisticsidleWorkerCountbusyWorkerCount)rstatss rworkerszThreadPool.workersns- %%'$$u'<'<<.NotAQueuecLjjjS)a Pretend to be a Python threading Queue and return the number of as-yet-unconsumed tasks. @return: the amount of backlogged work not yet dispatched to a worker. @rtype: L{int} )rFrKbackloggedWorkCount)qrs rrz*ThreadPool._queue..NotAQueue.qsizeszz,,.BBBrNr r#rsr NotAQueuerVs  CrrZr)rrZs` r_queuezThreadPool._queues C C{rcd|_d|_|j|jj j }|r|jj |yy)z' Start the threadpool. FTN)joinedrCadjustPoolsizerFrKrXgrow)rbacklogs rstartzThreadPool.startsN   **'')==  JJOOG $ rc:|jjdy)z Increase the number of available workers for the thread pool by 1, up to the maximum allowed by L{ThreadPool.max}. r2N)rFr_rs r startAWorkerzThreadPool.startAWorkers rcTd|jxs t|d|jS)z Generate a name for a new pool thread. @return: A distinctive name for the thread. @rtype: native L{str} z PoolThread--)r9idrOrs rr;zThreadPool._generateNames)TYY2"T(31T\\NCCrc:|jjdy)zn Decrease the number of available workers by 1, by quitting one as soon as it's idle. r2N)rFshrinkrs r stopAWorkerzThreadPool.stopAWorkers !rctt|d|tj||j|jy)N__dict__)setattrr-rIr)r*)rstates r __setstate__zThreadPool.__setstate__s(j%(D$((DHH5rcDt|j|jS)N)r)r*)r(r)r*rs r __getstate__zThreadPool.__getstate__s$((11rc2|jd|g|i|y)a Call a callable object in a separate thread. @param func: callable object to be called in separate thread @param args: positional arguments to be passed to C{func} @param kw: keyword args to be passed to C{func} N)callInThreadWithCallback)rfuncargsr?s r callInThreadzThreadPool.callInThreads  &%%dD>4>2>rc|jrytjjjddfd fd_|_|jjy)a$ Call a callable object in a separate thread and call C{onResult} with the return value, or a L{twisted.python.failure.Failure} if the callable raises an exception. The callable is allowed to block, but the C{onResult} function must not block and should perform as little work as possible. A typical action for C{onResult} for a threadpool used with a Twisted reactor would be to schedule a L{twisted.internet.defer.Deferred} to fire in the main reactor thread using C{.callFromThread}. Note that C{onResult} is called inside the separate thread, not inside the reactor thread. @param onResult: a callable with the signature C{(success, result)}. If the callable returns normally, C{onResult} is called with C{(True, result)} where C{result} is the return value of the callable. If the callable throws an exception, C{onResult} is called with C{(False, failure)}. Optionally, C{onResult} may be L{None}, in which case it is not called at all. @param func: callable object to be called in separate thread @param args: positional arguments to be passed to C{func} @param kw: keyword arguments to be passed to C{func} Nc j}d}d_jj||d_y|st j |yy#t$rt}d}Y]wxYw)NTF)theWork BaseExceptionronResultrerr)resultok inContexts rrz6ThreadPool.callInThreadWithCallback..inContext sy "**, !%I !!-""2v.%) "!   sAA32A3c8tjgiSr)rcall)rtctxrsr?srz5ThreadPool.callInThreadWithCallback..s%GLL % % "% rr!None) r]rtheContextTrackercurrentContextcontextsryr{rFdo)rr{rsrtr?rrs ```@@rrrz#ThreadPool.callInThreadWithCallbacksXH ;; ''668AA"E $ &  i rcd|_d|_|jj|jD]}|j y)z9 Shutdown the threads in the threadpool. TFN)r]rCrFquitr<join)rr@s rstopzThreadPool.stop$s<   ll F KKM rc| |j}| |j}|dk\sJd||ksJd||_||_|jsy|j|jkDr2|jj |j|jz |j|jkr3|jj |j|jz yy)z Adjust the number of available threads by setting C{min} and C{max} to new values. @param minthreads: The new value for L{ThreadPool.min}. @param maxthreads: The new value for L{ThreadPool.max}. Nrr6r7)r)r*rCrOrFrhr_)rrGrHs rr^zThreadPool.adjustPoolsize.s  J  JQ5 55Z'J)JJ'||  <<$(( " JJ  dllTXX5 6 <<$(( " JJOODHHt||3 4 #rctjd|jtjd|jtjd|jy)zw Dump some plain-text informational messages to the log about the state of this L{ThreadPool}. z waiters: z workers: ztotal: N)rmsgrSrQr<rs r dumpStatszThreadPool.dumpStatsMsI )DLL>*+ )DLL>*+ '$,,()r)r.r/N)rGr"rHr"r9z Optional[str]r )r!z list[None])r!rr)r!str)rmr(r!r)r!r()rszCallable[_P, object]rt_P.argsr? _P.kwargsr!r) r{z&Optional[Callable[[bool, _R], object]]rszCallable[_P, _R]rtrr?rr!r)NN)rG Optional[int]rHrr!r)#r$r%r&__doc__r)r*r]rCr9rr: staticmethodrrr currentThreadrErIpropertyrOrQrSr[rYrarcr;rirnrprurrrr^rrrrr-r-*s C C FG DM  Ir1a02   M  EPT"E"E/2"E?L"EH = =@@ @ @, A %D62 ?( ?18 ?@I ?  ??!8?!?! ?!  ?!  ?!BMQ5'5*rr-N)"r __future__r threadingrrtypingrrrr r r r r twisted._threadsrrEtwisted.pythonrrtwisted.python.deprecatertwisted.python.failurertwisted.python.versionsrrrrr(object WorkerStopr-rrrrsl #,9911*'/*+t_ T] X  Y X j*j*r