[Twisted-Python] blocking and threads

Joachim Boomberschloss boomberschloss at yahoo.com
Wed Apr 27 08:49:01 EDT 2005

Hi all,

I am just wondering: from the Twisted how-tos, it
appears that doing anything that could possibly take
arbitrarily long to execute should not be done in the
reactor's main thread; i.e. it should be done using an
asynchronous library (such as Twisted's network
communication facilities), or in a different thread,
using the reactor's thread pool.

So it would seem that many things that may be
considered "primitive" in Python, such as file IO,
require some kind of patching if they are to be
immediately usable by a Twisted application. I came up
with the following solution, which enables calling any
function in a different thread with a deferred
interface; I just wanted to make sure that I'm not
completely missing some point:

def deferToThread(func, *args, **kargs):
	"""executes the given function in a thread, and
passes the return value to the deferred we return"""
	d = defer.Deferred()
	reactor.callInThread(_calledInThread, d, func, *args,
	return d

def _calledInThread(d, func, *args, **kargs):
		retval = func(*args, **kargs)
	except Exception, x:
		reactor.callFromThread(d.errback, x)
		reactor.callFromThread(d.callback, retval)



Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 

More information about the Twisted-Python mailing list