[Twisted-Python] How to detect when there is nothing waiting in the event queue

Tim Allen screwtape at froup.com
Mon Sep 14 20:58:25 EDT 2009

On Mon, Sep 14, 2009 at 12:11:46PM -0400, Mark Visser wrote:
> I've added a signal handler to catch SIGHUP and call stop() on the 
> looping call and stopListening() on the perspective broker Root. How can 
> I detect when all remaining Deferreds have fired so I can stop the 
> reactor safely, without stranding any running processes?

I was looking at something like this for our production system a while
back; so far as I can tell, the only thing you can do is to maintain
a list of all 'live' deferreds as your process runs. When you get
the 'stop' signal, stop accepting incoming requests, put all the
deferreds into a DeferredList and don't exit until the DeferredList
fires. Our system makes it rather difficult to gather all possible
deferreds, and our processes only run for less than a minute, so we
decided to just put up with occasional lost processes.

As well as catching SIGHUP, you might want to investigate the reactor's
'addSystemEventTrigger' method, where you can queue calls to be called
at shutdown, and return a deferred that will cause the shutdown to be
delayed until the deferred fires.

More information about the Twisted-Python mailing list