[Twisted-Python] async code structure question

snacktime snacktime at gmail.com
Thu Feb 10 12:44:39 EST 2005

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.  What I need is a way to write back the data to the
client once all the callbacks have fired.  The code below doesn't
actually work because it prints a response to the client before the
actual response is even processed.

I can think of a couple of possible ways to deal with it.  One would
if there is a way to use transport.write from within Do() (Not sure if
that's possible), or maybe there is a way to call a method in the
protocol handler from inside another class?  Something like the latter
would be preferrable because I would like to keep the server separate
from the rest of the code if I can. Sometimes people will just want to
import ProcessTransaction and call Do() directly instead of using the
server component.

Does my problem make sense?  Not sure if I am explaining it very clearly.


class OT(Protocol):
    def dataReceived(self, data):
      c = ProcessTransaction()
      res = c.Do(data)
      self.transport.write("%s\r\n" % res)

application = service.Application("otransact")
OTService = service.IServiceCollection(application)
OTfactory = Factory()
OTfactory.protocol = OT
OTServer = internet.SSLServer(8000, OTfactory,ServerContextFactory())
print dir(OTServer)

More information about the Twisted-Python mailing list