How do i : Python Threads + KeyboardInterrupt exception

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Wed Jun 18 21:48:01 EDT 2008


En Wed, 18 Jun 2008 21:39:41 -0300, Brendon Costa <brendon at christian.net>  
escribió:

> I have a small python project i am working on. Basically i always have
> two threads. A "Read" thread that sits in a loop reading a line at a
> time from some input (Usually stdin) and then generating events to be
> processed and a "Proc" thread that processes incoming events from a
> queue. There will be additional threads as well that asynchronously
> insert events into the queue to be processed, but they are not a part
> of this so i have omitted them.
>
> What i want to know is: "What is the standard/best way of implementing
> such a pattern that works in the presence of errors and particularly
> with the KeyboardInterrupt exception?"

I don't know the "standard" way, but perhaps you can get some ideas from  
this recent thread:
http://groups.google.com/group/comp.lang.python/browse_thread/thread/82636f1bdd1d2d83/

> Some sample code is shown below. This code works as is, except in the
> case where the "Proc" thread wants to initiate the exit of the
> application.

You might try using the PyThreadState_SetAsyncExc function (from the  
Python C API) to inject a KeyboardInterrupt exception into the Read thread  
- but I don't know if it will work at all, the execution might be blocked  
waiting for an I/O call to complete, and never return to Python code...

-- 
Gabriel Genellina




More information about the Python-list mailing list