-1
import signal
import time

def sigint_handler(signum, frame):
    print 'User pressed CTRL+C!'

signal.signal(signal.SIGINT, sigint_handler)

def main():
    while True:
       print 'Script to handle SIGINT'
       time.sleep(2)

##########

if __name__ == "__main__":
    main()

How can I block this below exception thrown by python itself when executing the code:

File "D:\Documents\scripts\ctrlc handler.py", line 19, in <module> main() 
File "D:\Documents\scripts\ctrlc handler.py", line 14, in main 
time.sleep(2) OError: [Errno 4] Interrupted function call
Vignesh T
  • 1
  • 4
  • How to block this below exception thrown by python itself: – Vignesh T Feb 16 '18 at 08:48
  • File "D:\Documents\scripts\ctrlc handler.py", line 19, in main() File "D:\Documents\scripts\ctrlc handler.py", line 14, in main time.sleep(2) OError: [Errno 4] Interrupted function call – Vignesh T Feb 16 '18 at 08:48
  • 2
    Please don't split your question between the actual question and the comments. Also could you elaborate on how to reproduce said error? – FlyingTeller Feb 16 '18 at 08:51
  • is it really hard to do your own research https://stackoverflow.com/questions/1112343/how-do-i-capture-sigint-in-python just follow – L_Church Feb 16 '18 at 08:58
  • @L_Church I think the question is not really how to catch SIGINT (the code looks like the one in the accepted answer does it not?) but why time.sleep aborts with an OSError rather than the SIGINT being caught by the handler. Or am I missing something? – FlyingTeller Feb 16 '18 at 09:07
  • still wouldn't hurt to try that? it has 555 upvotes go for it lol – L_Church Feb 16 '18 at 09:08
  • My question is how to avoid the exception thrown by python like: "File "D:\Documents\scripts\ctrlc handler.py", line 19, in main() File "D:\Documents\scripts\ctrlc handler.py", line 14, in main" In above code, im able to print when ctrl+c is pressed, but i need to avoid above exception...I am new to posting question. sry about this @FlyingTeller – Vignesh T Feb 16 '18 at 10:38
  • I am able to handle signit, but i couldn't avoid python exception. So i posted here @L_Church – Vignesh T Feb 16 '18 at 10:43

1 Answers1

0

The issue seems to have something to do with different signals being received and different errors being thrown. See also this maybe for reference. The work around I could come up with was to catch and ignore the OSError/IOError that is being thrown like so:

import signal
import time

def sigint_handler(signum, frame):
    print 'User pressed CTRL+C!'

signal.signal(signal.SIGINT, sigint_handler)

def main():
    while True:
       print 'Script to handle SIGINT'
       try:
           time.sleep(2)
       except IOError:
           pass

##########

if __name__ == "__main__":
    main()

Which works perfectly.

Script to handle SIGINT
User pressed CTRL+C!
Script to handle SIGINT
User pressed CTRL+C!
Script to handle SIGINT
User pressed CTRL+C!
Script to handle SIGINT
User pressed CTRL+C!
Script to handle SIGINT
User pressed CTRL+C!
Script to handle SIGINT
User pressed CTRL+C!
Script to handle SIGINT
User pressed CTRL+C!
Script to handle SIGINT
User pressed CTRL+C!
FlyingTeller
  • 17,638
  • 3
  • 38
  • 53
  • added sys.exit(0) in sigint_handler function which is terminating the script as expected when ctrl+c is pressed...Thank u so much @FlyingTeller – Vignesh T Feb 16 '18 at 11:30