Stop SIGALRM when function returns

≯℡__Kan透↙ 提交于 2021-02-20 13:36:10


I have a problem that I can't seem to solve by myself. I'm writing a small python script and I would like to know why my signal.alarm still works after the function it's located in returned. Here is the code:

class AlarmException(Exception):

def alarmHandler(signum, frame):
    raise AlarmException

def startGame():
    import signal
    signal.signal(signal.SIGALRM, alarmHandler)
        # some code...
        return 1
    except AlarmException:
        # some code...
        return -1

def main():
    keepPlaying = True
    while keepPlaying:
        score = 0
        for level in range(1):
            score += startGame()
        answer = raw_input('Would you like to keep playing ? (Y/N)\n')
        keepPlaying = answer in ('Y', 'y')

So the problem is that when my startGame() function returns, the SIGALRM is still counting down and shutdown my program. Here is the traceback:

Would you like to keep playing ? (Y/N)
Traceback (most recent call last):
  File "", line 84, in <module>
  File "", line 80, in main
    answer = raw_input('Would you like to keep playing ? (Y/N)\n')
  File "", line 7, in alarmHandler
    raise AlarmException

How can I proceed to say to SIGALRM to stop when the function it is in has exited ?

Thanks !


Try calling signal.alarm(0) when you want to disable the alarm.

In all likelyhood it just calls alarm() in libc, and the man alarm says that alarm(0) "... voids the current alarm and the signal SIGALRM will not be delivered."