Search code examples
pythonmultithreadingtwisted

Twisted multi-thread, signal handling


I wrote a twisted program that handling request from TCP sockets and raw sockets. As the twisted doesn't support raw thread, I write the raw-socket select poll loop in a function named 'raw_socket_loop'. The main reactor program create a separate thread to run this loop by reactor.callInThread() function.

My problem is, I click control-C in the console but the reactor can not stop. I think the reactor's main thread receives this signal and handles well but the spawned thread doesn't receive this break signal. Is there is graceful shutdown suggestion for multi-thread reactor program?

Thanks a lot,


Solution

  • Threads aren't interruptable. You have to build a mechanism into the code running in a thread to receive shutdown notification and exit in response to it.

    If you're using select(2) in the thread, then you can use the self-pipe trick (which is how Twisted itself does this for its own thread-control needs).

    However, if you're using select(2) in a thread, then maybe you should consider not using a thread and instead implementing IFileDescriptor and using it with the reactor's IReactorFDSet implementation to get readiness events on it. This way you avoid threads, you let the reactor actually implement the event loop, and you still get your raw sockets.