7

In Windows I am trying to create a python process that waits for SIGINT signal.And when it receives SIGINT I want it to just print a message and wait for another occurrence of SIGINT.So I used signal handler.

Here is my signal_receiver.py code.

import signal, os, time def handler(signum, frame): print 'Yes , Received', signum signal.signal(signal.SIGINT, handler) print 'My process Id' , os.getpid() while True: print 'Waiting for signal' time.sleep(10) 

When this process running ,I just send SIGINT to this procees from some other python process using,

os.kill(pid,SIGINT).

But when the signal_receiver.py receives SIGINT it just quits the execution .But expected behavior is to print the message inside the handler function and continue execution.

Can some one please help me to solve this issue.Is it a limitation in windows ,because the same works fine in linux.

Thanks in advance.

3
  • Windows doesn't have signals. Python is emulating them for you, but might not support doing so across processes. Consider using one of the native IPC methods. Commented Sep 28, 2014 at 0:37
  • According to this answer, Python doesn't support cross-process signals. Commented Sep 28, 2014 at 0:44
  • Note that this program just works now on the latest version of python see docs.python.org/3/library/…. Commented Jun 15, 2022 at 14:28

1 Answer 1

4

When you press CTRL+C, the process receives a SIGINT and you are catching it correctly, because otherwise it would throw a KeyboardInterrupt error.

On Windows, when time.sleep(10) is interrupted, although you catch SIGINT, it still throws an InterruptedError. Just add a try/except statement inside time.sleep to catch this exception, for example:

import signal import os import time def handler(signum, frame): if signum == signal.SIGINT: print('Signal received') if __name__ == '__main__': print('My PID: ', os.getpid()) signal.signal(signal.SIGINT, handler) while True: print('Waiting for signal') try: time.sleep(5) except InterruptedError: pass 

Note: tested on Python3.x, it should also work on 2.x.

Sign up to request clarification or add additional context in comments.

4 Comments

@wap26 just replace InterruptedError with IOError and it should be ok.
I tested your example on Python 3.8.6 on a Lenovo laptop running Windows 10. No exception is thrown. The handler function runs in response to typing ctrl-c. Then the sleep completes after the programmed five seconds. Typing ctrl-fn-b kills the program via SIGBREAK, issuing a ^C message.
I further modified your example to print a message on SIGBREAK as well as SIGINT. It works, but the SIGBREAK message is delayed until the sleep completes, whereas the SIGINT message prints during the sleep. Also, now I have to go to another window to kill the process.
Note that this answer was correct for older versions of python. You no longer need to do the try catch around Interrupted Error. See docs.python.org/3/library/…

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.