[Twisted-web] How to wait for a deferred object?

Jean-Paul Calderone exarkun at divmod.com
Fri Dec 2 00:28:22 MST 2005


On Fri, 2 Dec 2005 12:54:48 +0800 (CST), Suet Lung Cheung <suetlung2003 at yahoo.com.hk> wrote:
>  Hi everyone,
>
> I am writing a TCP server program using twisted. I cannot use pb since the client side is not in python. I decided to use twisted.application and run it as internet.TCPServer(...). My problem is that I want to make it save something to a database whenever a connection closed. I do this in returning a deferred under Protocol.connectionLost(). I would like to make this work when the server is shut down. However when the server is down, it doesn't wait for the deferred to finish the callback and quit. I would like to make it save to db as normal but the server will wait for all deferred has called back.  Is there anyway I can do this? Any suggestions? Thanks a lot

Services receive shutdown notification via their stopService() method.  The method is called all the way down the service hierarchy when a twistd process is preparing to terminate.  If a stopService() method returns a Deferred, the Deferred will be allowed to fire before shutdown completes.  So you probably want a custom Service implementation which knows about the Deferreds from your connectionLost() and can return them from stopService() if necessary.

Jean-Paul



More information about the Twisted-web mailing list