[Twisted-Python] waitForDeferred Question

Mike Pelletier mike at mkp.ca
Sat Mar 11 01:24:59 EST 2006

At Fri, 10 Mar 2006 22:13:05 -0800,
Brian Granger wrote:
> In our system, we can easily imagine a user making dozens of
> interactive calls like this over short periods of time.  When all of
> these results are available essentially immediately, it iseems like
> overkill to have a more complicated UI.  But, it is very appropriate
> to use Twisted underneath, because it really is an asynchronous system
> and all the error detection and handling is best dealt with that
> asynchronously.  But as long as there are no errors, I woult like the
> Twisted machinery to just pass along the result directly.

It sounds like you need to teach your interpreter to deal with
deferreds.  I'd do something very vaugely like this:

def eval_loop(result):
  print result
  expression = raw_input(">>> ")
  d = maybeDeferred(eval(expression))
  d.addCallback(lambda r:reactor.callLater(eval_loop, r))

reactor.callWhenRunning(eval_loop, "Welcome to StupidPython")

Obviously this is not the correct way to implement an interpreter and
I don't know whether I've remembered all the names and signatures
correctly, but hopefully it conveys the idea.


More information about the Twisted-Python mailing list