[Twisted-Python] waitForDeferred Question

Brian Granger bgranger at scu.edu
Fri Mar 10 23:44:26 EST 2006

I probably should have given more details about what I am trying to
do.  Before I get going, I should mention that I have been using
Twisted heavily for 1.5 years and I have, for the most part, learned
to play the "Twisted Game."

So here is what I am working on:

I am developing objects (for scientific computing) that 1) use Twisted
to talk over the network and 2) need to be used interactively from a
python prompt.  This second point is really where the difficult and
interesting things are.

To allow Twisted-things to be used interactively I have built a
version of PyCrust/PyShell that is "Twisted enabled."  We have used
the standard threadedselectreactor to interleave the Twisted event
loop with that of PyShell.  We also inject the reactor into the users
interactive namespace.  Thus, you can use all of Twisted's
capabilities from an interactive python prompt.

As a side note, this situation is extremely nice for playing around
with Twisted and doing interactive debugging of Twisted-using

Here is a typical (madeup) usage case:

>>> a = TwistedEnabledObject()
>>> a.connect(addr)                # This uses reactor.connectTCP to
connect to a server
>>> myresult = a.computeSomethingUsingTwisted(args)     # This should block!!!
...and maybe...
>>> y = f(myresult)
>>> plot(y)

The key point is that the user will want to direclty use myresult
interactively.  Also they won't know ahead of time what actions they
will want to do.  Anyone who has used Matlab or Mathematica
(scientists) will find this way of working extremely familiar.  There
is simply no way that users will want to get a Deferred() and add
callbacks to trigger actions.  Thus I would argue that I DO want to
make my properly written asynchronous code blocking.

The implementation of computeSomethingUsingTwisted() is the difficult part:

class TwistedEnabledObject():

  def connect(addr):
    self.factory = MyClientFactory()
    self.connector = reactor.connectTCP(addr[0],addr[1],self.factory)

  def computeSomethingUsingTwisted(args):
    d = self.connector.transport.protocol.computeAndReturnDeferred(args)
    # Now I have a Deferred that will have the result, but I want to wait until
    # the result or an error is ready and then decide what the result should be
    result = # what to put here?
    return result

I don't think what I am doing goes against the "Twisted-way."  And my
needs are not coming from any inability on my part to write proper
asynchronous code.  The high level stuff here is really bbeing driven
by the need to use these object interactively.

On 3/10/06, glyph at divmod.com <glyph at divmod.com> wrote:

> If you don't mind some vitriol, there is more information here, on my blog: http://glyf.livejournal.com/40037.html

I am familiar with this and other similar "vitriols."  I fully agree
with most of what you say about threads.  But what does my usage case
have to do with threads?



Brian Granger
Santa Clara University
ellisonbg at gmail.com

More information about the Twisted-Python mailing list