[IPython-dev] Threads and Ctrl-C

Fernando Perez Fernando.Perez at colorado.edu
Fri Aug 19 14:58:36 EDT 2005

Hans Meine wrote:
> Hi!
> I don't know how many of you are actually working with IPython and GUI threads 
> - from my traceback experiences, I seem to be rather alone with my 
> strugglings.  (I guess other toolkits than Qt - which I use - should have 
> similar problems, but I might be wrong.)
> OK, try this:
> Start "ipython -qthread", enter:
> In [1]: import time
> In [2]: for i in range(60): time.sleep(1)
>    ...:
> Now press Ctrl-C, you will get a nice message "KeyboardInterrupt - Press 
> <Enter> to continue." which is more or less meaning "I don't know what to do 
> with your keypress, since I am totally unaware that the other thread might be 
> executing some code".
> In the before-mentioned Qt Python shell we wrote at our lab, we had a similar 
> situation (probably Toni will be interested in this), but we actually had two 
> separate processes for the GUI and Python, mostly to solve exactly this 
> single Ctrl-C problem:  As long as Python is doing something, the Ctrl-C 
> event would not get through to the GUI event handler, so the event handler 
> could not interrupt the interpreter.
> Ullrich Köthe's solution was to write a small "interrupt" module that is 
> triggered from the other process via a socket and interrupts the interpreter 
> by calling PyErr_SetInterrupt().  It's not too large; I attached it for your 
> inspiration.  IMHO having the possibility to interrupt some loop you 
> accidentally started with wrong parameters is a crucial feature?!

Thanks for the code, it will probably be useful in the future, as we move to a 
2-process model.  For a number of reasons (signal handling being only one), 
that is the right model in the long run, so I hope we can start moving in that 
direction sooner rather than later.



More information about the IPython-dev mailing list