How do i : Python Threads + KeyboardInterrupt exception

MRAB google at mrabarnett.plus.com
Thu Jun 19 10:25:41 EDT 2008


On Jun 19, 7:54 am, Dennis Lee Bieber <wlfr... at ix.netcom.com> wrote:
> On Wed, 18 Jun 2008 21:33:42 -0700 (PDT), Brendon Costa
> <bren... at christian.net> declaimed the following in comp.lang.python:
>
> > Unfortunately that is the problem. It is blocking in a IO system call
> > and i want to force it to exit that with an error, hopefully causing a
> > Python exception. I looked at what you mentioned and it is described a
> > bit here too:http://sebulba.wikispaces.com/recipe+thread2
>
>         There is no "safe" means of forcing a Python thread to exit... Heck,
> I believe the MSDN is full of warnings that killing a thread at the
> Windows OS level is fraught with danger.
>
>         Unfortunately, Windows doesn't allow for use of select() on anything
> other than sockets -- otherwise you could code a wait that included a
> "die" object and "write" to that object to break the blocking wait.
>
>         If your blocking call has no time-out variant, you may be stuck...
>
If only the main thread can receive KeyboardInterrupt, is there any
reason why you couldn't move the functionality of the Read thread into
the main thread? It looks like it's not doing any work, just waiting
for the Proc thread to finish.

You could start the Proc thread, do the current Read thread
functionality until the interrupt occurs, put the apporpriate message
in the queue, and then wait for the Proc thread to finish.



More information about the Python-list mailing list