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

exarkun at twistedmatrix.com exarkun at twistedmatrix.com
Tue Dec 8 10:14:06 EST 2009

On 12:50 pm, gabriel.rossetti at arimaz.com wrote:
>Hello everyone,
>I have to send a message just before my service quits, but sometimes 
>reactor quits before the msg is sent.
>I was wondering if I should user reactor.callLater like so :
>reactor.callLater(1, self.stop)
>(which I don't really like), or use reactor.callFromThread like so :
>(self.stop calls reactor.stop at some point).
>I really need for that msg to be sent before the reactor shuts down.

You should stop the reactor only after you receive acknowledgement that 
the message arrived at its destination.  Hopefully the protocol you're 
using supports this.

There are several ways in which bytes on a TCP connection can get lost. 
When you call reactor.stop() immediately after trying to send some 
bytes, it's most likely that they get lost because they are still in 
Twisted's send buffer.  However, even if they get out of Twisted's send 
buffer and into the kernel's, they still may get lost on the network. 
And even if they arrive in the destination's kernel receive buffer, the 
receiving process may still exit (perhaps unintentionally) without 
reading them.  And even if it reads them, it may exit without processing 
them.  And even if they are processed, the disk I/O which records the 
result of that processing may be interrupted by a power loss or similar 

You may not need to deal with all of these eventualities (it depends on 
the purpose and reliability requirements of your application, of 
course).  It's very common to want to know that the application on the 
other side of the connection read and processed the bytes, though.  This 
requires that the protocol allow messages to be acknowledged.


More information about the Twisted-Python mailing list