473,396 Members | 2,129 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 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 2392
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...
0
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.