decorator and signal handler

Discussion in 'Python' started by stalex, Sep 5, 2007.

  1. stalex

    stalex Guest

    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?
    stalex, Sep 5, 2007
    #1
    1. Advertising

  2. On Sep 5, 11:39 am, stalex <> 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
    Michele Simionato, Sep 5, 2007
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Michael Pronath
    Replies:
    1
    Views:
    1,144
    Diez B. Roggisch
    Jan 3, 2005
  2. Jack Orenstein

    threading.Thread vs. signal.signal

    Jack Orenstein, Sep 18, 2005, in forum: Python
    Replies:
    0
    Views:
    457
    Jack Orenstein
    Sep 18, 2005
  3. Weng Tianxiang
    Replies:
    2
    Views:
    642
    Jonathan Bromley
    Jan 30, 2007
  4. glomde
    Replies:
    5
    Views:
    515
    glomde
    Mar 29, 2007
  5. Nicolas Moreau
    Replies:
    9
    Views:
    3,098
Loading...

Share This Page