I'm running some computationally heavy simulation in (home-made) C-based python extensions. Occasionally I get stuff wrong and would like to terminate a simulation. However, Ctrl-C doesn't seem to have any effect (other than printing ^C
to the screen) so I have to kill the process using kill
or the system monitor.
As far as I can see python just waits for the C extension to finish and doesn't really communicate with it during this time.
Is there a way to make this work?
Update: The main answers (for my specific problem) turned out to be:
1. rewrite the code to regularly pass control back to the caller (answer Allowing Ctrl-C to interrupt a python C-extension below), or
2. Use PyErr_CheckSignals()
(answer https://stackoverflow.com/a/33652496/423420 below)
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…