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

exarkun at twistedmatrix.com exarkun at twistedmatrix.com
Mon Sep 14 13:16:32 MDT 2009


On 04:11 pm, markv at lumierevfx.com wrote:
>Hi, we've been using Twisted in an internal project for the past 4
>months. It's a system that polls for events from a web source (looping
>call) and also accepts connections via perspective broker. Each of 
>these
>events is turned into a series of Deferreds that run various tasks. 
>Some
>of these tasks are long-running and can take several hours to complete.
>
>Because of this, it's very difficult to restart the server when we roll
>out a new release.
>
>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?

There's no way to know when "all remaining Deferreds have fired".  Since 
Deferreds are general purpose and any part of Twisted may internally use 
them for anything they're suited for, this isn't really what you want 
anyway.  You want to know when all of *your* tasks have completed.  So, 
you should look at all the APIs you have which return Deferreds, decide 
which APIs need to be allowed to complete (perhaps it will be all of 
them) before shutdown can happen, and then instrument those APIs to keep 
track of their own status.  This may be as simple as internally adding a 
callback to the Deferreds being returned (before returning them), 
letting you know when each task has completed.  This should let you 
decide when things are quiet enough to shut down.

Jean-Paul




More information about the Twisted-Python mailing list