KeyboardInterrupt vs extension written in C

Donn Cave donn at drizzle.com
Fri Oct 21 01:39:55 EDT 2005


Quoth "Tamas Nepusz" <ntamas at gmail.com>:
| No, that's actually a bit more complicated. The library I'm working on
| is designed for performing calculations on large-scale graphs (~10000
| nodes and edges). I want to create a Python interface for that library,
| so what I want to accomplish is that I could just type "from igraph
| import *" in a Python command line and then access all of the
| functionalities of the igraph library. Now it works, except the fact
| that if, for example, I start computing the diameter of a random graph
| of ~100000 nodes and ~200000 edges, I can't cancel it, because the
| KeyboardInterrupt is not propagated to the Python toplevel (or it isn't
| even generated until the igraph library routine returns). I would like
| to allow the user to cancel the computation with the usual Ctrl-C and
| return to the Python interactive interface.
| This whole feature is not vital, but it would mean a big step towards
| user-friendliness.
| I have access to the source code of igraph as well (since one of my
| colleagues is developing it), so another possible solution would be to
| inject some calls into the igraph source code which temporarily cancels
| the computation and checks whether there's something waiting in the
| Python interpreter, but I haven't found any function in the API which
| allows me to do this.

Hm, tough question.  Even in C, this would be a little awkward.
If you only wanted the program to abort on ^C, then you could just
replace the default sigint handler with SIG_DFL.  But then you'd be
back to the shell prompt, not the python prompt.  If you want to
catch the signal, but also abort a computation, then as you say,
the computation needs to check periodically.

Rather than peek into Python for this, I'm wondering if your module
could set its own signal handler for SIGINT, which would set a library
flag.  Then call PyErr_SetInterrupt(), to emulate the normal signal
handler.

	Donn Cave, donn at drizzle.com



More information about the Python-list mailing list