connect SIGINT to custom interrupt handler
Hi, I am trying to connect SIGINT (^c) to a custom interrupt handler like this (no threading, just straightforward): if __name__ == "__main__": quit = False def interrupt_handler(signal, frame): global quit if not quit: print "blabla, i'll finish my task and quit kind of message" print "Press ^C again to interrupt immediately." else: sys.exit(2) quit = True signal.signal(signal.SIGINT, interrupt_handler) # main will use the quit flag to determine if it should quit before next # task status = main() This worked fine in some rare lucky cases, but most of the times, the module I am using (my university's seismology project) catches the SIGINT and quits: select.error: (4, 'Interrupted system call') How can I prevent the imported module's function from catching the interrupt signal? Thanks to anyone that takes the time to help me... Chris -- Chris Scheingraber - www.scheingraber.net -- http://mail.python.org/mailman/listinfo/python-list
Re: connect SIGINT to custom interrupt handler
I now have signal.siginterrupt(signal.SIGINT, False) in the line below signal.signal(signal.SIGINT, interrupt_handler) Unfortunately, pressing ^c still results in the same interrupt error. I also tried putting signal.siginterrupt into the interrupt_handler function, which gave an interesting result: File "/usr/local/bin/obspysod", line 586, in interrupt_handler signal.siginterrupt(signal.SIGINT, False) AttributeError: 'int' object has no attribute 'siginterrupt' Could there be a namespace problem? On 2011-05-15, Nobody wrote: > On Sun, 15 May 2011 09:44:04 +, Christoph Scheingraber wrote: > >> signal.signal(signal.SIGINT, interrupt_handler) > >> This worked fine in some rare lucky cases, but most of the times, the >> module I am using (my university's seismology project) catches the SIGINT >> and quits: >> >> select.error: (4, 'Interrupted system call') > > After installing the signal handler, call: > > signal.siginterrupt(signal.SIGINT, False) > > This will cause (most) interrupted system calls to be restarted after the > signal has been handled. > >> How can I prevent the imported module's function from catching the >> interrupt signal? > > It isn't catching the signal. Unless you enable restarting of system > calls, an interrupted system call will typically fail with EINTR. Python > typically reports failures via exceptions; failures due to EINTR aren't > handled differently. > -- Chris Scheingraber - www.scheingraber.net -- http://mail.python.org/mailman/listinfo/python-list
Re: connect SIGINT to custom interrupt handler
On 2011-05-15, Thomas 'PointedEars' Lahn wrote: > > Obviously. `signal' refers to an `int' object, probably by something like > > signal = 42 > > before. E.g. `print' or a debugger will tell you, as you have not showed > the relevant parts of the code. The problem is that I am running someone else's module which seems to use signal, I guess that means I have to create a child method? Is it correct anyway to have signal.siginterrupt(signal.SIGINT, False) in my custom interrupt_handler function or should it be outside but after signal.signal(signal.SIGINT, interrupt_handler)? > > Please trim your quotes to the relevant minimum; DO NOT top-post. > Also, it is not acceptable behavior to use domain namespaces without > authorization (ch...@spam.org is not a mailbox, yet spam.org is > registered to someone else). > I am sorry, I changed it to my own domain. -- Chris Scheingraber - www.scheingraber.net -- http://mail.python.org/mailman/listinfo/python-list
Re: connect SIGINT to custom interrupt handler
On 2011-05-15, Miki Tebeka wrote: > Why not just catch KeyboardInterrupt? Would it be possible to continue my program as nothing had happened in that case (like I did before, setting a flag to tell main() to finish the running data download and quit instead of starting the next data download {it's a for-loop})? I have tried it, but after catching the KeyboardInterrupt I could only continue to the next iteration. -- http://mail.python.org/mailman/listinfo/python-list