[Numpy-discussion] Handling interrupts in NumPy extensions
David Cournapeau
david at ar.media.kyoto-u.ac.jp
Fri Aug 25 00:39:23 EDT 2006
Travis Oliphant wrote:
>
> Right, as long as you know what to do you are O.K. I was just thinking
> about a hypothetical situation where the library allocated some
> temporary memory that it was going to free at the end of the subroutine
> but then an interrupt jumped out back to your code before it could
> finish. In a case like this, you would have to use the "check if
> interrupt has occurred" approach before and after the library call.
Indeed.
By the way, I tried something for python.thread + signals. This is posix
specific, and it works as expected on linux:
- first, a C extension which implements the signal handling. It has a
function called hello, which is the entry point of the C module, and
calls the function process (which does random computation). It checks if
it got a SIGINT signal, and returns -1 if caught. Returns 0 if no SIGINT
called:
- extension compiled into python module (I used boost python because I
am too lazy to find how to do it in C :) )
- python script which creates several threads running the hello
function. They run in parallel, and ctrl+C is correctly handled. I think
this is signal specific, and this needs to be improved (this is just
meant as a toy example):
import threading
import hello
import time
class mythread(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
def run(self):
print "Starting thread", self.getName()
st = 0
while st == 0:
st = hello.foo(self.getName())
# sleep to force the python interpreter to run
# other threads if available
time.sleep(1)
if st == -1:
print self.getName() + " got signal"
print "Ending thread", self.getName()
nthread = 5
t = [mythread() for i in range(nthread)]
[i.start() for i in t]
Then, you have something like:
tarting thread Thread-1
Thread-1 processing... done
clean called
Starting thread Thread-5
Thread-5 processing... done
clean called
Starting thread Thread-3
Thread-3 processing... done
clean called
Starting thread Thread-2
Thread-2 processing... done
hello.c:hello signal caught line 56 for thread Thread-2
clean called
Thread-1 processing... done
clean called
Starting thread Thread-4
Thread-4 processing... done
clean called
Thread-5 processing... done
clean called
Thread-3 processing... done
hello.c:hello signal caught line 56 for thread Thread-3
clean called
Thread-2 got signal
Ending thread Thread-2
Thread-1 processing... done
clean called
Thread-4 processing... done
clean called
Thread-5 processing... done
clean called
Thread-3 got signal
Ending thread Thread-3
Thread-1 processing... done
hello.c:hello signal caught line 56 for thread Thread-1
clean called
Thread-4 processing... done
clean called
Thread-5 processing... done
hello.c:hello signal caught line 56 for thread Thread-5
clean called
Thread-1 got signal
Ending thread Thread-1
Thread-4 processing... done
clean called
Thread-5 got signal
Ending thread Thread-5
Thread-4 processing... done
clean called
Thread-4 processing... done
clean called
Thread-4 processing... done
hello.c:hello signal caught line 56 for thread Thread-4
clean called
Thread-4 got signal
Ending thread Thread-4
(SIGINT are received when Ctrl+C on linux)
You can find all sources here:
http://www.ar.media.kyoto-u.ac.jp/members/david/numpysig/
Please note that I know almost nothing about all this stuff, I just
naively implemented from the example of GNU C library, and it always
worked for me on matlab on my machine. I do not know if this is
portable, if this can work for other signals, etc...
David
More information about the NumPy-Discussion
mailing list