Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #73893

threading.Condition.wait() is not catching SIGTERM

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed1a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <sangeeth.saravanaraj@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.006
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; 'subject:not': 0.03; '"__main__":': 0.09; '__name__': 0.09; 'executed': 0.09; 'main()': 0.09; 'def': 0.12; 'creates': 0.14; 'a()': 0.16; 'block.': 0.16; 'blocks': 0.16; 'called.': 0.16; 'handlers': 0.16; 'main():': 0.16; 'received:192.168.128': 0.16; 'respond.': 0.16; 'variable': 0.18; 'all,': 0.19; 'import': 0.22; 'print': 0.22; 'skip:_ 20': 0.27; 'tried': 0.27; 'code': 0.31; 'terminate': 0.31; 'class': 0.32; 'skip:_ 10': 0.34; 'skip:s 30': 0.35; 'received:google.com': 0.35; 'doing': 0.36; 'method': 0.36; 'charset:us-ascii': 0.36; 'message-id:@gmail.com': 0.38; 'thank': 0.38; 'to:addr:python- list': 0.38; 'to:addr:python.org': 0.39; 'called': 0.40; 'signal': 0.60; 'mentioned': 0.61; 'header:Message-Id:1': 0.63; 'it!': 0.67; 'registers': 0.68; 'here!': 0.84; 'interrupted': 0.96
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:content-type:subject:message-id:date:to:mime-version; bh=a8FiqKESQeBFS76yG//TJmpjz/DaykQBBuy7uvRFYYc=; b=paXLWI5x/5mDr1IiThkJkekstqXfttCh6QNv9HApo7A3IzFjK/vVfTUG1h9tmo4oPh HztJ3wuQcmZJ+Z5g0QK3Jl/VP8Fkwazz+OyNdb85Dqjdr6HuxgiRMZPBZLRrVYNw2j5+ 8Y/EbPxWwS96iWm1HvHeIpqW7LvePKFMQ7UpyQI/n0UtHX4FuhlhJIGrnnqSrPIcLd6D WDQlzfSmcgPmNZI8ysXh6VUsf9VikhtuxVJbS/67tEo45T+UiqpJVa96cvDf/IAB3eRp XNd/x6w628+XsH7yaGY0C9OhjTdQYBttMVro/UIBtEcdWbeZw1BGXKhqAqU0zqvQLyxG PKgw==
X-Received by 10.66.234.202 with SMTP id ug10mr3574686pac.109.1404382543393; Thu, 03 Jul 2014 03:15:43 -0700 (PDT)
From Sangeeth Saravanaraj <sangeeth.saravanaraj@gmail.com>
Content-Type multipart/alternative; boundary="Apple-Mail=_7F87B4DE-9EBA-4CD1-A203-73BF6243FB3B"
Subject threading.Condition.wait() is not catching SIGTERM
Date Thu, 3 Jul 2014 15:45:38 +0530
To python-list@python.org
Mime-Version 1.0 (Mac OS X Mail 7.3 \(1878.2\))
X-Mailer Apple Mail (2.1878.2)
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.11450.1404382552.18130.python-list@python.org> (permalink)
Lines 130
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1404382552 news.xs4all.nl 2908 [2001:888:2000:d::a6]:39628
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:73893

Show key headers only | View raw


[Multipart message — attachments visible in raw view] - view raw

Hi All, 

I have the following code which when executed waits to be interrupted by SIGINT, SIGTERM or SIGQUIT. When an object is initialized, it creates a threading.Condition() and acquires() it! The program then registers the signal handlers where notify() and release() is called when the above mentioned signals are received. After registering the signal handlers, it calls wait() on the condition variable and block.

When I tried to stop the program with Ctrl-C, its did not respond. IOW, the _signal_handler() method did not get called.  

# start

from signal import signal, SIGINT, SIGTERM, SIGQUIT
from threading import Condition

class A:
    def __init__(self):
        self._termination_signal = Condition()
        self._termination_signal.acquire(blocking=0)

    def _signal_handler(self, signum, frame):
        print "Received terminate request - signal = {0}".format(signum)
        del frame
        self._termination_signal.notify()
        self._termination_signal.release()
        return

    def register_and_wait(self):
        signal(SIGINT, self._signal_handler)
        signal(SIGTERM, self._signal_handler)
        signal(SIGQUIT, self._signal_handler)
        print "Waiting to be interrupted!"
        self._termination_signal.wait()      # control blocks here!
        print "Notified!!"

def main():
    a = A()
    a.register_and_wait()

if __name__ == "__main__":
    main()

# end

What am I doing wrong?! 

Thank you,

Sangeeth

Back to comp.lang.python | Previous | NextNext in thread | Find similar | Unroll thread


Thread

threading.Condition.wait() is not catching SIGTERM Sangeeth Saravanaraj <sangeeth.saravanaraj@gmail.com> - 2014-07-03 15:45 +0530
  Re: threading.Condition.wait() is not catching SIGTERM Roy Smith <roy@panix.com> - 2014-07-03 06:19 -0400
    Re: threading.Condition.wait() is not catching SIGTERM Sangeeth Saravanaraj <sangeeth.saravanaraj@gmail.com> - 2014-07-03 15:57 +0530
    Re: threading.Condition.wait() is not catching SIGTERM Chris Angelico <rosuav@gmail.com> - 2014-07-03 20:33 +1000

csiph-web