[Twisted-Python] async code structure question
Itamar Shtull-Trauring
itamar at itamarst.org
Thu Feb 10 11:08:15 MST 2005
On Thu, 2005-02-10 at 09:44 -0800, snacktime wrote:
> Thanks for the info Mary. Now that I've gotten a bit further I have
> another design issue that I really don't know how to deal with.
>
> The below code is my main server loop. When data is received I call a
> non blocking method (Do()). So far so good. Inside Do() I have a
> number of reactor.callLater's that fire off blocking database calls
> inside adbapi and are chained using callbacks. Do() returns
> immediately.
Do() should return a Deferred that has result when processing is done,
and then you can Do().addCallback(lambda: self.transport.write("hello"))
or something.
e.g.
def Do():
d = deferToThread(foo)
d.addCallback(processResult)
return d
or
def Do():
# processing in this case is waiting for one second
d = Deferred()
reactor.callLater(d.callback, 1)
return d
More information about the Twisted-Python
mailing list