Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > decorator and signal handler

Reply
Thread Tools

decorator and signal handler

 
 
stalex
Guest
Posts: n/a
 
      09-05-2007
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?

 
Reply With Quote
 
 
 
 
Michele Simionato
Guest
Posts: n/a
 
      09-05-2007
On Sep 5, 11:39 am, stalex <shao...@gmail.com> wrote:
> 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
No Signal and Signal 'Low' MapleE. Wireless Networking 0 03-20-2010 08:32 PM
Why doesnt __getattr__ with decorator dont call __get_method in decorator glomde Python 5 03-29-2007 02:48 PM
Event Handler that creates adds another event handler kaczmar2@gmail.com ASP .Net 1 02-22-2007 07:37 AM
Aside from delta cycles and/or resolution functions, how can the effective value of a signal differ from a driving signal of its? Colin Paul Gloster VHDL 0 01-11-2007 01:31 PM
how do u invoke Tag b's Tag Handler from within Tag a's tag Handler? shruds Java 1 01-27-2006 03:00 AM



Advertisments