o b'@sxdZddlmZmZddlmZddlmZddl m Z m Z ddl m Z ddlmZddlmZeZGd d d Zd S) 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. )Threadcurrent_thread)List)pool)contextlog) deprecated)Failure)Versionc@seZdZdZdZdZdZdZdZe Z e e e dddd d d eZe eZd,d d ZeddZeddZeddZeddZeZddZddZddZddZddZd d!Zd"d#Zd$d%Zd&d'Z d-d(d)Z!d*d+Z"dS). 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 replacementcsb|dksJd||ksJd|_|_|_g_fdd}fdd}||_dS) 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 maximumcs(j|di|}j||S)Nname) threadFactory _generateNamethreadsappend)akwthreadself;/usr/lib/python3/dist-packages/twisted/python/threadpool.pytrackingThreadFactoryLs z2ThreadPool.__init__..trackingThreadFactorycsjsdSjS)Nr)startedmaxrrrr currentLimitSsz)ThreadPool.__init__..currentLimitN)minr#rr_pool_team)r minthreads maxthreadsrr!r$rrr __init__8s   zThreadPool.__init__cCs|j}|j|jS)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} )r' statisticsidleWorkerCountbusyWorkerCount)rstatsrrr workersZs zThreadPool.workerscCdg|jjS)z For legacy compatibility purposes, return the number of busy workers as expressed by a list the length of that number. @return: the number of workers currently processing a work item. @rtype: L{list} of L{None} N)r'r+r-rrrr workingfs zThreadPool.workingcCr0)a, For legacy compatibility purposes, return the number of idle workers as expressed by a list the length of that number. @return: the number of workers currently alive (with an allocated thread) but waiting for new work. @rtype: L{list} of L{None} N)r'r+r,rrrr waitersqs zThreadPool.waiterscsGfddd}|S)z For legacy compatibility purposes, return an object with a C{qsize} method that indicates the amount of work not yet allocated to a worker. @return: an object with a C{qsize} method. cseZdZfddZdS)z$ThreadPool._queue..NotAQueuecs jjS)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} )r'r+backloggedWorkCount)qrrr qsizes z*ThreadPool._queue..NotAQueue.qsizeN)__name__ __module__ __qualname__r5rrrr NotAQueuesr9r)rr9rrr _queue}s  zThreadPool._queuecCs8d|_d|_||jj}|r|j|dSdS)z' Start the threadpool. FTN)joinedr"adjustPoolsizer'r+r3grow)rbacklogrrr starts zThreadPool.startcC|jddS)z Increase the number of available workers for the thread pool by 1, up to the maximum allowed by L{ThreadPool.max}. rN)r'r=rrrr startAWorkerzThreadPool.startAWorkercCsd|jpt|d|jS)z Generate a name for a new pool thread. @return: A distinctive name for the thread. @rtype: native L{str} z PoolThread--)ridr/rrrr rszThreadPool._generateNamecCr@)zn Decrease the number of available workers by 1, by quitting one as soon as it's idle. rN)r'shrinkrrrr stopAWorkerrBzThreadPool.stopAWorkercCs"t|d|t||j|jdS)N__dict__)setattrr r*r%r#rstaterrr __setstate__s zThreadPool.__setstate__cCsi}|j|d<|j|d<|S)Nr%r#)r%r#rIrrr __getstate__s  zThreadPool.__getstate__cOs|jd|g|Ri|dS)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)rfuncargsrrrr callInThreads zThreadPool.callInThreadcsP|jrdStjjdfddfdd_|_|jdS)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} Ncsjz}d}Wntyt}d}Ynwd_jdur*||d_dS|s3t|dSdS)NTF)theWork BaseExceptionr onResultrerr)resultok) inContextrr rXs    z6ThreadPool.callInThreadWithCallback..inContextcstjgRiS)N)rcallr)rOctxrNrrr sz5ThreadPool.callInThreadWithCallback..) r;rtheContextTrackercurrentContextcontextsrRrTr'do)rrTrNrOrr)rOrZrNrXrr rMs z#ThreadPool.callInThreadWithCallbackcCs.d|_d|_|j|jD]}|qdS)z9 Shutdown the threads in the threadpool. TFN)r;r"r'quitrjoin)rrrrr stop s    zThreadPool.stopcCs|dur|j}|dur|j}|dksJd||ksJd||_||_|js)dS|j|jkr9|j|j|j|j|jkrK|j|j|jdSdS)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}. Nrrr)r%r#r"r/r'rEr=)rr(r)rrr r<s   zThreadPool.adjustPoolsizecCs:td|jtd|jtd|jdS)zw Dump some plain-text informational messages to the log about the state of this L{ThreadPool}. z waiters: z workers: ztotal: N)rmsgr2r1rrrrr dumpStats2szThreadPool.dumpStats)r r N)NN)#r6r7r8__doc__r%r#r;r"rrr staticmethodrr r currentThreadr&r*propertyr/r1r2r:r4r?rArrFrKrLrPrMrbr<rdrrrr r sL  "       ; r N)re threadingrrtypingrtwisted._threadsrr&twisted.pythonrrtwisted.python.deprecatertwisted.python.failurer twisted.python.versionsr object WorkerStopr rrrr s