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


Groups > comp.lang.python > #73893 > unrolled thread

threading.Condition.wait() is not catching SIGTERM

Started bySangeeth Saravanaraj <sangeeth.saravanaraj@gmail.com>
First post2014-07-03 15:45 +0530
Last post2014-07-03 20:33 +1000
Articles 4 — 3 participants

Back to article view | Back to comp.lang.python


Contents

  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

#73893 — threading.Condition.wait() is not catching SIGTERM

FromSangeeth Saravanaraj <sangeeth.saravanaraj@gmail.com>
Date2014-07-03 15:45 +0530
Subjectthreading.Condition.wait() is not catching SIGTERM
Message-ID<mailman.11450.1404382552.18130.python-list@python.org>

[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

[toc] | [next] | [standalone]


#73894

FromRoy Smith <roy@panix.com>
Date2014-07-03 06:19 -0400
Message-ID<roy-A625DF.06190003072014@news.panix.com>
In reply to#73893
In article <mailman.11450.1404382552.18130.python-list@python.org>,
 Sangeeth Saravanaraj <sangeeth.saravanaraj@gmail.com> wrote:

> Hi All, 
> 
> I have the following code which when executed waits to be interrupted by 
> SIGINT, SIGTERM or SIGQUIT.

We need more information.  What version of Python are you using?  And, 
what operating system is this running on?

[toc] | [prev] | [next] | [standalone]


#73895

FromSangeeth Saravanaraj <sangeeth.saravanaraj@gmail.com>
Date2014-07-03 15:57 +0530
Message-ID<mailman.11451.1404383271.18130.python-list@python.org>
In reply to#73894
On 03-Jul-2014, at 3:49 pm, Roy Smith <roy@panix.com> wrote:

> In article <mailman.11450.1404382552.18130.python-list@python.org>,
> Sangeeth Saravanaraj <sangeeth.saravanaraj@gmail.com> wrote:
> 
>> Hi All, 
>> 
>> I have the following code which when executed waits to be interrupted by 
>> SIGINT, SIGTERM or SIGQUIT.
> 
> We need more information.  What version of Python are you using?  And, 
> what operating system is this running on?

I am using Python 2.7.7 and running this code on MacOS Darwin Kernel 13.2.0

But does the behavior of threading.Condition.wait() depends on operating system?! 

> -- 
> https://mail.python.org/mailman/listinfo/python-list

[toc] | [prev] | [next] | [standalone]


#73896

FromChris Angelico <rosuav@gmail.com>
Date2014-07-03 20:33 +1000
Message-ID<mailman.11453.1404383638.18130.python-list@python.org>
In reply to#73894
On Thu, Jul 3, 2014 at 8:27 PM, Sangeeth Saravanaraj
<sangeeth.saravanaraj@gmail.com> wrote:
> But does the behavior of threading.Condition.wait() depends on operating system?!

The behaviour of signals certainly does - there's a huge difference
between Windows and POSIX, and there are lesser differences between
Linux and Mac OS, and so on. It's just safest to say exactly what
platform, in case it matters.

ChrisA

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web