/usr/lib/python3/dist-packages/twisted/internet
NameSizeModeActions
iocpreactor/-0755rm
test/-0755rm
__pycache__/-0755rm
abstract.py192740644editdlrm
address.py52460644editdlrm
asyncioreactor.py105530644editdlrm
base.py442600644editdlrm
cfreactor.py175010644editdlrm
default.py19420644editdlrm
defer.py711930644editdlrm
endpoints.py772070644editdlrm
epollreactor.py84930644editdlrm
error.py126450644editdlrm
fdesc.py32230644editdlrm
gireactor.py61230644editdlrm
glib2reactor.py11150644editdlrm
gtk2reactor.py36140644editdlrm
gtk3reactor.py22520644editdlrm
inotify.py146910644editdlrm
interfaces.py965150644editdlrm
kqreactor.py102920644editdlrm
main.py10540644editdlrm
pollreactor.py60260644editdlrm
posixbase.py263570644editdlrm
process.py388960644editdlrm
protocol.py271340644editdlrm
pyuisupport.py8170644editdlrm
reactor.py18630644editdlrm
selectreactor.py62150644editdlrm
serialport.py23170644editdlrm
ssl.py84440644editdlrm
stdio.py10450644editdlrm
task.py311190644editdlrm
tcp.py549080644editdlrm
threads.py39530644editdlrm
tksupport.py20450644editdlrm
udp.py185690644editdlrm
unix.py219650644editdlrm
utils.py78760644editdlrm
win32eventreactor.py151930644editdlrm
wxreactor.py52630644editdlrm
wxsupport.py13630644editdlrm
_baseprocess.py19130644editdlrm
_dumbwin32proc.py131070644editdlrm
_glibbase.py128130644editdlrm
_idna.py13970644editdlrm
_newtls.py93320644editdlrm
_pollingfile.py89820644editdlrm
_posixserialport.py19950644editdlrm
_posixstdio.py46870644editdlrm
_producer_helpers.py37880644editdlrm
_resolver.py85300644editdlrm
_signals.py27180644editdlrm
_sslverify.py717700644editdlrm
_threadedselect.py117640644editdlrm
_win32serialport.py47440644editdlrm
_win32stdio.py32040644editdlrm
__init__.py5210644editdlrm
Edit: /usr/lib/python3/dist-packages/twisted/internet/threads.py (3953B)
# Copyright (c) Twisted Matrix Laboratories. # See LICENSE for details. """ Extended thread dispatching support. For basic support see reactor threading API docs. """ from __future__ import division, absolute_import from twisted.python.compat import _PY3 if not _PY3: import Queue else: import queue as Queue from twisted.python import failure from twisted.internet import defer def deferToThreadPool(reactor, threadpool, f, *args, **kwargs): """ Call the function C{f} using a thread from the given threadpool and return the result as a Deferred. This function is only used by client code which is maintaining its own threadpool. To run a function in the reactor's threadpool, use C{deferToThread}. @param reactor: The reactor in whose main thread the Deferred will be invoked. @param threadpool: An object which supports the C{callInThreadWithCallback} method of C{twisted.python.threadpool.ThreadPool}. @param f: The function to call. @param *args: positional arguments to pass to f. @param **kwargs: keyword arguments to pass to f. @return: A Deferred which fires a callback with the result of f, or an errback with a L{twisted.python.failure.Failure} if f throws an exception. """ d = defer.Deferred() def onResult(success, result): if success: reactor.callFromThread(d.callback, result) else: reactor.callFromThread(d.errback, result) threadpool.callInThreadWithCallback(onResult, f, *args, **kwargs) return d def deferToThread(f, *args, **kwargs): """ Run a function in a thread and return the result as a Deferred. @param f: The function to call. @param *args: positional arguments to pass to f. @param **kwargs: keyword arguments to pass to f. @return: A Deferred which fires a callback with the result of f, or an errback with a L{twisted.python.failure.Failure} if f throws an exception. """ from twisted.internet import reactor return deferToThreadPool(reactor, reactor.getThreadPool(), f, *args, **kwargs) def _runMultiple(tupleList): """ Run a list of functions. """ for f, args, kwargs in tupleList: f(*args, **kwargs) def callMultipleInThread(tupleList): """ Run a list of functions in the same thread. tupleList should be a list of (function, argsList, kwargsDict) tuples. """ from twisted.internet import reactor reactor.callInThread(_runMultiple, tupleList) def blockingCallFromThread(reactor, f, *a, **kw): """ Run a function in the reactor from a thread, and wait for the result synchronously. If the function returns a L{Deferred}, wait for its result and return that. @param reactor: The L{IReactorThreads} provider which will be used to schedule the function call. @param f: the callable to run in the reactor thread @type f: any callable. @param a: the arguments to pass to C{f}. @param kw: the keyword arguments to pass to C{f}. @return: the result of the L{Deferred} returned by C{f}, or the result of C{f} if it returns anything other than a L{Deferred}. @raise: If C{f} raises a synchronous exception, C{blockingCallFromThread} will raise that exception. If C{f} returns a L{Deferred} which fires with a L{Failure}, C{blockingCallFromThread} will raise that failure's exception (see L{Failure.raiseException}). """ queue = Queue.Queue() def _callFromThread(): result = defer.maybeDeferred(f, *a, **kw) result.addBoth(queue.put) reactor.callFromThread(_callFromThread) result = queue.get() if isinstance(result, failure.Failure): result.raiseException() return result __all__ = ["deferToThread", "deferToThreadPool", "callMultipleInThread", "blockingCallFromThread"]