Signal Handling in Windows

2024/9/24 15:22:34

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, timedef handler(signum, frame):print 'Yes , Received', signumsignal.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.

Answer

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 timedef 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.

https://en.xdnf.cn/q/71685.html

Related Q&A

Python tkinter.filedialog askfolder interfering with clr

Im mainly working in Spyder, building scripts that required a pop-up folder or file Browse window.The code below works perfect in spyder. In Pycharm, the askopenfilename working well, while askdirector…

Run a function for each element in two lists in Pandas Dataframe Columns

df: col1 [aa, bb, cc, dd] [this, is, a, list, 2] [this, list, 3]col2 [[ee, ff, gg, hh], [qq, ww, ee, rr]] [[list, a, not, 1], [not, is, this, 2]] [[this, is, list, not], [a, not, list, 2]]What Im tryin…

cannot filter palette images error when doing a ImageEnhance.Sharpness()

I have a GIF image file. I opened it using PIL.Image and did a couple of size transforms on it. Then I tried to use ImageSharpness.Enhance() on it...sharpener = PIL.ImageEnhance.Sharpness(img) sharpene…

Is there a PyPi source download link that always points to the lastest version?

Say my latest version of a package is on PyPi and the source can be downloaded with this url:https://pypi.python.org/packages/source/p/pydy/pydy-0.3.1.tar.gzId really like to have a url that looks like…

Can this breadth-first search be made faster?

I have a data set which is a large unweighted cyclic graph The cycles occur in loops of about 5-6 paths. It consists of about 8000 nodes and each node has from 1-6 (usually about 4-5) connections. Im d…

How to remove rows of a DataFrame based off of data from another DataFrame?

Im new to pandas and Im trying to figure this scenario out: I have a sample DataFrame with two products. df = Product_Num Date Description Price 10 1-1-18 Fruit Snacks 2.9910 1-2-18 …

Amazon S3 Python S3Boto 403 Forbidden When Signature Has + sign

I am using Django and S3Boto and whenever a signature has a + sign in it, I get a 403 Forbidden. If there is no + sign in the signature, I get the resource just fine. What could be wrong here?UPDATE: …

List comparison of element

I have a question and it is a bit hard for me to explain so I will be using lots of examples to help you all understand and see if you could help me.Say I have two lists containing book names from best…

Partition pyspark dataframe based on the change in column value

I have a dataframe in pyspark. Say the has some columns a,b,c... I want to group the data into groups as the value of column changes. SayA B 1 x 1 y 0 x 0 y 0 x 1 y 1 x 1 yThere will be 3 grou…

Error group argument must be None for now in multiprocessing.pool

Below is my python script.import multiprocessing # We must import this explicitly, it is not imported by the top-level # multiprocessing module. import multiprocessing.pool import timefrom random impor…