Signal handler doesn`t handle any signals while main thread is blocked

Discussion in 'Python' started by Grzegorz Kokosiñski, May 26, 2008.

  1. Hi!
    I waste a lot of my time figuring why this snip of code doesn`t work.
    And still without any solution ;/
    Does anybody know why this handler cannot handle SIGINT signal? What
    did I wrong?

    Thanks in advance.

    #
    # CODE
    #
    import signal
    from threading import *

    def siginthandler(signum, frame):
    global work,cv
    print 'terminating...'
    cv.acquire()
    work = 0
    cv.notifyAll ()
    cv.release()

    work = 1
    signal.signal (signal.SIGINT, siginthandler)

    print 'wait for SIGINT signal'
    cv = Condition ()
    cv.acquire()
    while work:
    print 'waiting...'
    cv.wait()
    print 'woke up'
    cv.release()

    print 'terminated'
     
    Grzegorz Kokosiñski, May 26, 2008
    #1
    1. Advertising

  2. Grzegorz Kokosiñski

    Chris Guest

    Re: Signal handler doesn`t handle any signals while main thread isblocked

    On May 26, 2:26 pm, "Grzegorz Kokosiñski" <>
    wrote:
    > Hi!
    > I waste a lot of my time figuring why this snip of code doesn`t work.
    > And still without any solution ;/
    > Does anybody know why this handler cannot handle SIGINT signal? What
    > did I wrong?
    >
    > Thanks in advance.
    >
    > #
    > # CODE
    > #
    > import signal
    > from threading import *
    >
    > def siginthandler(signum, frame):
    >      global work,cv
    >      print 'terminating...'
    >      cv.acquire()
    >      work = 0
    >      cv.notifyAll ()
    >      cv.release()
    >
    > work = 1
    > signal.signal (signal.SIGINT, siginthandler)
    >
    > print 'wait for SIGINT signal'
    > cv = Condition ()
    > cv.acquire()
    > while work:
    >     print 'waiting...'
    >     cv.wait()
    > print 'woke up'
    > cv.release()
    >
    > print 'terminated'


    change cv.wait() to cv.wait(delay) and it will capture the terminate
    request and wrap the cv.wait(delay) in a try/except IOError: break
    block.
     
    Chris, May 26, 2008
    #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. Jack Orenstein

    threading.Thread vs. signal.signal

    Jack Orenstein, Sep 18, 2005, in forum: Python
    Replies:
    0
    Views:
    469
    Jack Orenstein
    Sep 18, 2005
  2. Lloyd Zusman
    Replies:
    0
    Views:
    296
    Lloyd Zusman
    Jan 11, 2007
  3. Maligree
    Replies:
    2
    Views:
    356
  4. RVince
    Replies:
    1
    Views:
    383
    Chris Rebert
    Jul 25, 2011
  5. Glenn
    Replies:
    5
    Views:
    268
    C.DeRykus
    Jan 3, 2011
Loading...

Share This Page