Scripting *of* Python

Andrew Bennetts andrew-pythonlist at puzzling.org
Fri Jan 31 04:38:07 CET 2003


On Thu, Jan 30, 2003 at 07:08:01PM -0800, Erik Max Francis wrote:
> Phil Mayers wrote:
> 
> > It's a contrived example, but I'm sure you see what I mean - the
> > function
> > cannot block for very long, so it has to queue itself. This code
> > quickly
> > gets unreadable (the code it *would* be in a thread is over 1k lines!)
> > and
> > non-technical (well, less technical) people have to be able to write
> > it.
> 
> As much as I prefer to recommend this as rarely as possible, it sounds
> like a threaded approach might really be in order.  This sounds really
> like the case where an asynchronous framework (whether it's Medusa or
> Twisted or whatever) is going to be have problems because you need to do
> a lot of work in between asynchronous I/O polls, and the only
> alternative is breaking the work up into chunks which then makes it much
> harder to read, debug, and maintain.

That doesn't preclude the use of Twisted, though.

    from twisted.internet.threads import deferToThread
    d = deferToThread(func, *args, **kwargs)
    d.addCallback(myProtocol.sendResult)
    d.addErrback(myProtocol.sendError)

This way you can keep doing your network I/O with Twisted, and your long
running calculations in a thread, if you really need to.  Twisted's DB-API
wrapper, twisted.enterprise.adbapi, works this way.

-Andrew.






More information about the Python-list mailing list