Threading Keyboard Interrupt issue

eryk sun eryksun at gmail.com
Wed May 29 21:08:28 EDT 2019


On 5/29/19, Dennis Lee Bieber <wlfraed at ix.netcom.com> wrote:
>
> 	In the OP's example code, with just one thread started, the easiest
> solution is to use
>
> 	y.start()
> 	y.join()
>
> to block the main thread. That will, at least, let the try/except catch the
> interrupt. It does not, however, kill the sub-thread.

join() can't be interrupted by Ctrl+C in Windows. To work around this,
we can join a thread with a short timeout in a loop. If we're managing
queued work items with a thread pool, note that Queue.join doesn't
support a timeout. In this case we need to poll empty() in a loop with
a short time.sleep(). Or we can let the main thread block and use
ctypes to install a console control handler that sets a flag that
tells child threads to exit. Windows calls the control handler in a
new thread.



More information about the Python-list mailing list