[Twisted-Python] blocking and threads

Joachim Boomberschloss boomberschloss at yahoo.com
Wed Apr 27 08:07:15 MDT 2005


Thanks, how embarassing... I suggest adding it to the
"using threads" howto (I'd do it myself if I knew
how). Another idea: make an asynchronous file-handling
module that wraps Python's file IO with something that
has a deferred-oriented interface. Is there already
something like that too?

Joe

--- Jp Calderone <exarkun at divmod.com> wrote:
> On Wed, 27 Apr 2005 05:49:01 -0700 (PDT), Joachim
> Boomberschloss <boomberschloss at yahoo.com> wrote:
> >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,
> >**kargs)
> >	return d
> >
> >def _calledInThread(d, func, *args, **kargs):
> >	try:
> >		retval = func(*args, **kargs)
> >	except Exception, x:
> >		reactor.callFromThread(d.errback, x)
> >	else:
> >		reactor.callFromThread(d.callback, retval)
> >
> 
>   Looks like you've got a pretty firm understanding.
>  The one thing you did miss is the
> twisted.internet.threads module, which provides the
> deferToThread function.
> 
>   Jp
> 
> _______________________________________________
> Twisted-Python mailing list
> Twisted-Python at twistedmatrix.com
>
http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python
> 

__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 




More information about the Twisted-Python mailing list