[Twisted-Python] Exit all threads upon KeyboardInterrupt

exarkun at twistedmatrix.com exarkun at twistedmatrix.com
Tue Jan 14 04:12:18 MST 2014


On 13 Jan, 11:31 pm, glyph at twistedmatrix.com wrote:
>
>On Jan 12, 2014, at 4:42 PM, johnnadre <johnnadre at zoho.com> wrote:
>>
>>---- On Sun, 12 Jan 2014 14:03:24 -0800 <exarkun at twistedmatrix.com> 
>>wrote ----
>>>On 09:01 pm, johnnadre at zoho.com wrote:
>>>>
>>>>Hi,
>>>>
>>>>I want to exit my application immediately when CTRL+C is pressed,
>>>>however reactor hangs when there are running threads.
>>>>Some of these threads have blocking I/O, so I can't simply set a
>>>>variable or wait for them to terminate. An example application would
>>>>be:
>>>
>>>Python threads (being plain old operating systems; for example, POSIX
>>>threads) aren't generally interruptable.  You could try exiting the
>>>entire process using `os._exit`.
>
>Well, there's always pthread_kill, now exposed as os.pthread_kill on 
>Python 3.3, but ... don't do that.  Nothing good will come of it.
>>>This is the case whether you're using Twisted or not.
>>
>>Thanks! That's exactly what I was looking for.
>
>If you need to use os._exit, your application probably has a bug :-).
>
>(Not to say you should never use it, but any time you use it you should 
>be simultaneously looking into why you need to.)
>
>-glyph

I think you may as well say the same about needing to use threads.  The 
needs are pretty closely related, anyway.

Consider an application that uses twisted.enterprise.adbapi to talk to a 
SQL server, is running when the network suffers a failure that leaves a 
cursor waiting for data from the server, and then the user decides they 
want to exit the application.

Most likely the cursor will block forever on a recv() call, the thread 
it runs in will never proceed, and the process will never exit.

Whether this is a bug or an unavoidable consequence of doing blocking 
I/O using POSIX threads or both...

Jean-Paul



More information about the Twisted-Python mailing list