1
# Copyright (c) 2001-2010 Twisted Matrix Laboratories.
2
# See LICENSE for details.
5
Extended thread dispatching support.
7
For basic support see reactor threading API docs.
9
Maintainer: Itamar Shtull-Trauring
14
from twisted.python import failure
15
from twisted.internet import defer
18
def deferToThreadPool(reactor, threadpool, f, *args, **kwargs):
20
Call the function C{f} using a thread from the given threadpool and return
21
the result as a Deferred.
23
This function is only used by client code which is maintaining its own
24
threadpool. To run a function in the reactor's threadpool, use
27
@param reactor: The reactor in whose main thread the Deferred will be
30
@param threadpool: An object which supports the C{callInThreadWithCallback}
31
method of C{twisted.python.threadpool.ThreadPool}.
33
@param f: The function to call.
34
@param *args: positional arguments to pass to f.
35
@param **kwargs: keyword arguments to pass to f.
37
@return: A Deferred which fires a callback with the result of f, or an
38
errback with a L{twisted.python.failure.Failure} if f throws an
43
def onResult(success, result):
45
reactor.callFromThread(d.callback, result)
47
reactor.callFromThread(d.errback, result)
49
threadpool.callInThreadWithCallback(onResult, f, *args, **kwargs)
54
def deferToThread(f, *args, **kwargs):
56
Run a function in a thread and return the result as a Deferred.
58
@param f: The function to call.
59
@param *args: positional arguments to pass to f.
60
@param **kwargs: keyword arguments to pass to f.
62
@return: A Deferred which fires a callback with the result of f,
63
or an errback with a L{twisted.python.failure.Failure} if f throws
66
from twisted.internet import reactor
67
return deferToThreadPool(reactor, reactor.getThreadPool(),
71
def _runMultiple(tupleList):
73
Run a list of functions.
75
for f, args, kwargs in tupleList:
79
def callMultipleInThread(tupleList):
81
Run a list of functions in the same thread.
83
tupleList should be a list of (function, argsList, kwargsDict) tuples.
85
from twisted.internet import reactor
86
reactor.callInThread(_runMultiple, tupleList)
89
def blockingCallFromThread(reactor, f, *a, **kw):
91
Run a function in the reactor from a thread, and wait for the result
92
synchronously, i.e. until the callback chain returned by the function
95
@param reactor: The L{IReactorThreads} provider which will be used to
96
schedule the function call.
97
@param f: the callable to run in the reactor thread
98
@type f: any callable.
99
@param a: the arguments to pass to C{f}.
100
@param kw: the keyword arguments to pass to C{f}.
102
@return: the result of the callback chain.
103
@raise: any error raised during the callback chain.
105
queue = Queue.Queue()
106
def _callFromThread():
107
result = defer.maybeDeferred(f, *a, **kw)
108
result.addBoth(queue.put)
109
reactor.callFromThread(_callFromThread)
111
if isinstance(result, failure.Failure):
112
result.raiseException()
116
__all__ = ["deferToThread", "deferToThreadPool", "callMultipleInThread",
117
"blockingCallFromThread"]