Interrupting a running python thread

J

johan2sson

I have embedded a python console in a plugin to a Windows application.
I do not control the application, but the plugin is mine. Since the
application is not thread-safe, I am push()-ing each line onto a
code.InteractiveConsole on the GUI thread. This has the downside that a
silly mistake that creates an infinite loop also hangs the GUI, thus
preventing you from saving your work or exiting the application in any
normal fashion.

As a workaround, I've installed a console handler that calls
thread.interrupt_main() when control-c is pressed. I should point out
that this is done on a new thread. Unfortunately this doesn't work at
all. Without returning from the last call to InteractiveConsole.push()
or catching the KeyboardInterrupt, the interpreter explodes with a "no
current thread" fatal error.

I've tried the exact same code at "the real" python prompt and that
happily prints:

Traceback (most recent call last):

The code used is this infinite loop:

a = 0
while a == 0: b = a + 2

What's the secret? The only use of SetConsoleCtrlHandler I could find
in the CPython source didn't make me much wiser.

Johan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top