Your IP : 172.28.240.42


Current Path : /usr/lib/python2.7/dist-packages/twisted/python/
Upload File :
Current File : //usr/lib/python2.7/dist-packages/twisted/python/threadpool.pyc

ó
[³XMc@s±dZddlZddlZddlZddlZddlZddlmZmZm	Z	ddl
mZddlm
Z
eƒZdd
d„ƒYZddd	„ƒYZdS(s°
twisted.threadpool: a pool of threads to which we dispatch tasks.

In most cases you can just use reactor.callInThread and friends
instead of creating a thread pool directly.
iÿÿÿÿN(tlogtcontexttfailure(tdeprecatedModuleAttribute(tVersiont
ThreadPoolcBséeZdZdZdZeZeZdZdZ
ejZ
eejƒZdddd„Zd„Zd„Zd„Zd„Zd	„Zd
„Zd„Zd„Zd
„Zd„Zd„Zd„Zd„Zddd„Zd„ZRS(s
    This class (hopefully) generalizes the functionality of a pool of
    threads to which work can be dispatched.

    callInThread() and stop() should only be called from
    a single thread, unless you make a subclass where stop() and
    _startSomeWorkers() are synchronized.
    iiicCs||dkstdƒ‚||ks0tdƒ‚tjdƒ|_||_||_||_g|_g|_g|_dS(s®
        Create a new threadpool.

        @param minthreads: minimum number of threads in the pool

        @param maxthreads: maximum number of threads in the pool
        isminimum is negativesminimum is greater than maximumN(	tAssertionErrortQueuetqtmintmaxtnametwaiterstthreadstworking(tselft
minthreadst
maxthreadsR((s=/usr/lib/python2.7/dist-packages/twisted/python/threadpool.pyt__init__0s					cCs t|_t|_|jƒdS(s'
        Start the threadpool.
        N(tFalsetjoinedtTruetstartedtadjustPoolsize(R((s=/usr/lib/python2.7/dist-packages/twisted/python/threadpool.pytstartBs		cCsj|jd7_d|jp$t|ƒ|jf}|jd|jd|ƒ}|jj|ƒ|jƒdS(NisPoolThread-%s-%sttargetR(tworkersRtidt
threadFactoryt_workerR
tappendR(RRt	newThread((s=/usr/lib/python2.7/dist-packages/twisted/python/threadpool.pytstartAWorkerKs
"cCs#|jjtƒ|jd8_dS(Ni(Rtputt
WorkerStopR(R((s=/usr/lib/python2.7/dist-packages/twisted/python/threadpool.pytstopAWorkerRscCs&||_tj||j|jƒdS(N(t__dict__RRR	R
(Rtstate((s=/usr/lib/python2.7/dist-packages/twisted/python/threadpool.pyt__setstate__Vs	cCs$i}|j|d<|j|d<|S(NR	R
(R	R
(RR%((s=/usr/lib/python2.7/dist-packages/twisted/python/threadpool.pyt__getstate__Zs

cCsL|jjƒt|jƒ}x)|jt|j|ƒkrG|jƒqWdS(N(RtqsizetlenRRR	R
R (Rt
neededSize((s=/usr/lib/python2.7/dist-packages/twisted/python/threadpool.pyt_startSomeWorkers`scOs-tjdtddƒ|j|||ŽdS(sp
        DEPRECATED: use L{callInThread} instead.

        Dispatch a function to be a run in a thread.
        sFdispatch() is deprecated since Twisted 8.0, use callInThread() insteadt
stackleveliN(twarningstwarntDeprecationWarningtcallInThread(Rtownertfunctargstkw((s=/usr/lib/python2.7/dist-packages/twisted/python/threadpool.pytdispatchgs	
cOs|jd|||ŽdS(sý
        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 func

        @param **kw: keyword args to be passed to func
        N(tcallInThreadWithCallbacktNone(RR2R3R4((s=/usr/lib/python2.7/dist-packages/twisted/python/threadpool.pyR0ss
cOsb|jr
dStjjƒjd}|||||f}|jj|ƒ|jr^|jƒndS(sæ
        Call a callable object in a separate thread and call 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 onResult function
        must not block and should perform as little work as possible.

        A typical action for onResult for a threadpool used with a
        Twisted reactor would be to schedule a Deferred to fire in the
        main reactor thread using C{.callFromThread}.  Note that
        onResult is called inside the separate thread, not inside the
        reactor thread.

        @param onResult: a callable with the signature (success, result).
            If the callable returns normally, onResult is called with
            (True, result) where result is the return value of the callable.
            If the callable throws an exception, onResult is called with
            (False, failure).

            Optionally, onResult may be 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 func

        @param **kwargs: keyword arguments to be passed to func
        Niÿÿÿÿ(	RRttheContextTrackertcurrentContexttcontextsRR!RR+(RtonResultR2R3R4tctxto((s=/usr/lib/python2.7/dist-packages/twisted/python/threadpool.pyR6€s		cCsByt|||ƒ}Wn|tjƒdƒnX||ƒdS(Ni(tapplytsystexc_info(RtcallbackterrbackR2R3tkwargstresult((s=/usr/lib/python2.7/dist-packages/twisted/python/threadpool.pyt_runWithCallback§s
cOs9tjdtddƒ|j|j|||||ƒdS(s
        DEPRECATED: use L{twisted.internet.threads.deferToThread} instead.

        Dispatch a function, returning the result to a callback function.

        The callback function will be called in the thread - make sure it is
        thread-safe.
        smdispatchWithCallback() is deprecated since Twisted 8.0, use twisted.internet.threads.deferToThread() instead.R,iN(R-R.R/R0RE(RR1RARBR2R3R4((s=/usr/lib/python2.7/dist-packages/twisted/python/threadpool.pytdispatchWithCallback°s		
c
Csg|jƒ}|jjƒ}x5|tk	rR|jj|ƒ|\}}}}}~y"tj||||Ž}t}	WnAt	}	|dkr¨tj|tjƒd}q¸t
jƒ}nX~~~|jj|ƒ|dk	rytj|||	|ƒWqtj|tjƒqXn~~~|jj|ƒ|jjƒ}|jj|ƒqW|jj|ƒdS(s»
        Method used as target of the created threads: retrieve task to run
        from the threadpool, run it, and proceed to the next task until
        threadpool is stopped.
        N(t
currentThreadRtgetR"RRRtcallRRR7RterrRtFailuretremoveRR
(
RtctR=R<tfunctionR3RCR;RDtsuccess((s=/usr/lib/python2.7/dist-packages/twisted/python/threadpool.pyRÁs6
			cCsit|_tj|jƒ}x,|jrI|jjtƒ|jd8_qWx|D]}|jƒqQWdS(s9
        Shutdown the threads in the threadpool.
        iN(	RRtcopyR
RRR!R"tjoin(RR
tthread((s=/usr/lib/python2.7/dist-packages/twisted/python/threadpool.pytstopës	
cCsÓ|dkr|j}n|dkr0|j}n|dksHtdƒ‚||ks`tdƒ‚||_||_|jsdSx |j|jkr¡|jƒq‚Wx |j|jkrÄ|jƒq¥W|jƒdS(Nisminimum is negativesminimum is greater than maximum(	R7R	R
RRRR#R R+(RRR((s=/usr/lib/python2.7/dist-packages/twisted/python/threadpool.pyRús			cCsWtjd|jjƒtjd|jƒtjd|jƒtjd|jƒdS(Ns	queue: %sswaiters: %ssworkers: %ss	total: %s(RtmsgRtqueueRRR
(R((s=/usr/lib/python2.7/dist-packages/twisted/python/threadpool.pyt	dumpStatssN( t__name__t
__module__t__doc__R	R
RRRRR7Rt	threadingtThreadRtstaticmethodRGRRR R#R&R'R+R5R0R6RERFRRSRRV(((s=/usr/lib/python2.7/dist-packages/twisted/python/threadpool.pyRs2										
	'				*	tThreadSafeListcBsTeZdZeeddddƒdedƒd„Zd„Zd	„Zd
„ZRS(sÏ
    In Jython 2.1 lists aren't thread-safe, so this wraps it.  Newer versions
    of Jython are completely different than 2.1, so this class is deprecated
    to make way for future versions of Jython.
    tTwistedi
iis\This was an internal implementation detail of support for Jython 2.1, which is now obsolete.R]cCstjƒ|_g|_dS(N(RZtLocktlocktl(R((s=/usr/lib/python2.7/dist-packages/twisted/python/threadpool.pyR&scCs6|jjƒz|jj|ƒWd|jjƒXdS(N(R`tacquireRaRtrelease(Rti((s=/usr/lib/python2.7/dist-packages/twisted/python/threadpool.pyR*s
cCs6|jjƒz|jj|ƒWd|jjƒXdS(N(R`RbRaRLRc(RRd((s=/usr/lib/python2.7/dist-packages/twisted/python/threadpool.pyRL1s
cCs
t|jƒS(N(R)Ra(R((s=/usr/lib/python2.7/dist-packages/twisted/python/threadpool.pyt__len__8s(	RWRXRYRRRRRLRe(((s=/usr/lib/python2.7/dist-packages/twisted/python/threadpool.pyR]s
			(((RYRRZRPR?R-ttwisted.pythonRRRttwisted.python.deprecateRttwisted.python.versionsRtobjectR"RR](((s=/usr/lib/python2.7/dist-packages/twisted/python/threadpool.pyt<module>s	ü