python - Stop SIGALRM when function returns -
i have problem can't seem solve myself. i'm writing small python script , know why signal.alarm
still works after function it's located in returned. here code:
class alarmexception(exception): pass def alarmhandler(signum, frame): raise alarmexception def startgame(): import signal signal.signal(signal.sigalrm, alarmhandler) signal.alarm(5) try: # some code... return 1 except alarmexception: # code... return -1 def main(): printheader() keepplaying = true while keepplaying: score = 0 level in range(1): score += startgame() answer = raw_input('would keep playing ? (y/n)\n') keepplaying = answer in ('y', 'y')
so problem when startgame()
function returns, sigalrm still counting down , shutdown program. here traceback:
would keep playing ? (y/n) traceback (most recent call last): file "game.py", line 84, in <module> main() file "game.py", line 80, in main answer = raw_input('would keep playing ? (y/n)\n') file "game.py", line 7, in alarmhandler raise alarmexception __main__.alarmexception
how can proceed sigalrm stop when function in has exited ?
thanks !
try calling signal.alarm(0)
when want disable alarm.
in likelyhood calls alarm()
in libc, , man alarm
says alarm(0)
"... voids current alarm , signal sigalrm not delivered."
Comments
Post a Comment