Question

How does one catch signals (specifically/especially SIGINT) in a translated RPython program?

I've grepped through the PyPy source tree, I found the CPython signal module, and attempting to use this in the translated program gives the expected error during translation:

[translation:ERROR]  Exception: unexpected prebuilt constant: <built-in function signal>

There is also pypy.module.signal but I don't know if that's what I should be using, or how to use it. That module does provide a signal function, but it takes an additional space parameter; these space parameters appear a few places through the source tree and I can't work out what they refer to, or what I should pass there.

Background: I'm looking to have a computation that runs for an arbitrary length of time, but capture ^C so that I can print out the results so far before the program exits. (The obvious try: ... except KeyboardInterrupt: ... around the main loop doesn't work either.)

Was it helpful?

Solution

You'll have to use the RPython signal handling functions, which are a little wrapper over the platform ones. The functions you'll need are in pypy.module.signal.interp_signal. For example, pypysig_ignore(signum) is equivalent to signal(signum, SIG_IGN). Call pypysig_poll to return the pending signal if there is one. Here's a excerpt to ask for the signal to be handled and wait for it to arrive

pypysig_setflag(signum)
while True:
    n = pypysig_poll()
    if n < 0:
        # No signals pending
        break
    if n == signum:
        handle_signal()
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top