472,982 Members | 2,494 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,982 software developers and data experts.

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(sid, f)
return f
return handler

class Test(object):
@sigHandler(signal.SIGTERM)
def _sigHandler(self, 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 2321
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.SIGTERM)
def termination(frame):
print 'sent SIGTERM'
raise SystemExit

Code calling termination.signal() 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_decorator(handler):
'Install the handler and add a .signal function attribute to
it'
signal.signal(sig, lambda signum, frame : handler(frame))
handler.signal = lambda : os.kill(os.getpid(), sig)
return handler
return handler_decorator

Michele Simionato

Sep 5 '07 #2

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

Similar topics

2
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...
2
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...
3
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.*/...
4
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...
3
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...
11
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
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...
5
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...
9
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
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...

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.