[Twisted-Python] reactor.callLater() or reactor.callFromThread() when sending msg just before quit

Itamar Turner-Trauring (aka Shtull-Trauring) itamar at itamarst.org
Tue Dec 8 10:20:24 EST 2009

On Tue, 2009-12-08 at 13:50 +0100, Gabriel Rossetti wrote:
> I have to send a message just before my service quits, but sometimes the 
> reactor quits before the msg is sent.
> I was wondering if I should user reactor.callLater like so :
> (which I don't really like), or use reactor.callFromThread like so :

Neither of these will necessarily work. Instead, you want to use a
shutdown hook:

reactor.addSystemEventTrigger("before", "shutdown", f)

when you do reactor.stop(), f will be called... and shutdown will be
delayed until the Deferred f returns is triggered. In your case, send
message, tell connection to close, and then stop reactor. If you make
sure the Deferred from f only gets callback()ed when connectionLost is
called you'll get behavior you want.

More information about the Twisted-Python mailing list