473,785 Members | 2,261 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

decorator and signal handler

Hi all,

I wrote the following code since I want to try using a decorator to
install signal handler:

## The test.py script
############### ##
import os
import time
import signal

def sigHandler(sid) :
def handler(f):
signal.signal(s id, f)
return f
return handler

class Test(object):
@sigHandler(sig nal.SIGTERM)
def _sigHandler(sel f, signalId, currentFrame):
print "Received:" , signalId

if __name__ == "__main__":
print "pid:", os.getpid()

t = Test()
time.sleep(300)

# From terminal, say A
############### #####
$ python test.py
pid: 1234

# From terminal, say B
############### ####
$ kill -TERM 1234

After issuing the kill command from terminal B, the process of test.py
from terminal A is terminated.
Python print out some exception message as following:
Traceback (most recent call last):
File "a.py", line 22, in <module>
time.sleep(300)
TypeError: _sigHandler() takes exactly 3 arguments (2 given)

At a guess, I think the decorator I defined did not pass the 'self' as
the first argument to _sigHandler() method, did it? And I have no idea
of how to write a correct one. Any suggestion?

Sep 5 '07 #1
1 2444
On Sep 5, 11:39 am, stalex <shao...@gmail. comwrote:
Hi all,

I wrote the following code since I want to try using a decorator to
install signal handler:
I do have a decorator for exactly that purpose in my code. Here it is:

def on(sig):
'''
A factory of decorators for signal handlers. An example of usage
is

@on(signal.SIGT ERM)
def termination(fra me):
print 'sent SIGTERM'
raise SystemExit

Code calling termination.sig nal() will send a SIGTERM signal to
the
main thread, which in turns will call the termination handler,
which
will print a message and raise SystemExit.
'''
def handler_decorat or(handler):
'Install the handler and add a .signal function attribute to
it'
signal.signal(s ig, lambda signum, frame : handler(frame))
handler.signal = lambda : os.kill(os.getp id(), sig)
return handler
return handler_decorat or

Michele Simionato

Sep 5 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
3968
by: Gary Robinson | last post by:
In some code we're writing we're making an assumption, and I'd like to confirm that the assumption is valid. Suppose a signal arrives while a file is being written, and the signal handler explicitly raises a SystemExit exception. My understanding is that the C-level I/O code will continue and the signal won't be processed until the end of the atomic python interpreter instruction that invoked that C code.
2
2212
by: lpw | last post by:
I have dilligently reviewed FAQ-lite Section 3.2, "How do I pass a pointer-to-member-function to a signal handler, X event callback, system call that starts a thread/task, etc." The only suggestion on how to deliver a signal to an object is to do it via a global variable and a wrapper function, a technique that is generally a Bad Idea (due to the usage of a global variable). I understand that this ng is dedicated to the discussion of...
3
3526
by: Martin McCormick | last post by:
A C program contains several signal statements to remove a lock file if the program gets killed: /*Set up interrupt handler to catch ctrl-C so that lock file can be removed.*/ signal(SIGINT,crash); signal(SIGBUS,crash); signal(SIGSEGV,crash); This part works. Those signals cause the "crash.c" module to run and get rid of the lock. Is there a standard way to also print
4
2018
by: Eric Boutin | last post by:
Hi ! currently reading C docs, I think I'm reading docs about the stdc lib, but I'm not shure.. it talks about signals, does *all* OS implement the signal function, and use it well ? I mean.. I know Unixes do it, but what about windows ? does it implements all SIG* signal promptly ? Thanks ! -Eric Boutin
3
2680
by: LeTubs | last post by:
Hi I'm not sure if this is correct ...place to post but here goes This is what i'm trying to do, I want to write a signal / alarm handler ( I don't know which hence the posting here, as once I know the proper name /useage then I'll use google to try and find out more information ).... Thus any tips or pointers would be helpful (and I'm using a UNIX variant as OS)
11
2975
by: Jackie | last post by:
Hi everyone, I'd like to know when and how signals are used (e.g. SIGFPE, SIGABRT, SIGTERM, SIGSEGV, SIGINT)? Thank you so much.
18
3920
by: Sven | last post by:
Hi, I found a strange behaviour when using the time() function from time.h. Sometimes when it is called, it does not show the correct time in seconds, but an initial value. This time seem to be the time when the program was started the first time. My platform is a DEC machine with Tru64 onboard. A possible explanation could be, that the time() function is called
5
6651
by: david | last post by:
I'm developing a program that runs using an asyncore loop. Right now I can adequately terminate it using Control-C, but as things get refined I need a better way to stop it. I've developed another program that executes it as a child process using popen2.Popen4(). I was attempting to use signals to stop it (using os.kill()) but I keep running into a problem where sending the signal causes an infinite loop of printing the message...
9
5838
by: thagor2008 | last post by:
Is the behaviour of throwing exceptions in a unix signal handler defined? eg: void sighandler(int sig) { ... do something throw myobj; }
0
9646
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9484
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9957
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8983
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6742
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5386
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5518
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4055
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2887
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.