signal and threading

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • ~levon

    signal and threading

    Hello group,

    in following example, a signal handler is registered and a thread
    started. if I call self.doSomethin () directly
    the code works as I would expect. as i send a SIGINT shutdown is
    called and the script terminates.

    as soon as I call doSomething() in a thread the the SIGINT handler is
    never called again and
    i have to terminate the script with a SIGTERM or SIGKILL.

    well, i would expect the handler to be called in both cases, am i
    missing something?

    by the way. calling os.kill(os.getp id(), signal.SIGINT) works as I
    would expect, what
    don't is kill -s SIGINT pid # where pid is the actual process id

    the code:

    class Runner(object):
    def __init__(self):
    print os.getpid()
    self.shd = False
    signal.signal(s ignal.SIGINT, self.shutdown)
    threading.Threa d(target=self.d oSomething).sta rt()
    # the following works fine:
    #os.kill(os.get pid(), signal.SIGINT)

    def doSomething(sel f):
    while not self.shd:
    pass

    def shutdown(self, signo, frm):
    self.shd = True

    if __name__ == '__main__':
    Runner()

    ~levon
  • ~levon

    #2
    Re: signal and threading

    this seems to be the solution:


    On Aug 25, 3:37 pm, "~levon" <Levon.Ghazar.. .@gmail.comwrot e:
    Hello group,
    >
    in following example, a signal handler is registered and a thread
    started. if I call self.doSomethin () directly
    the code works as I would expect. as i send a SIGINT shutdown is
    called and the script terminates.
    >
    as soon as I call doSomething() in a thread the the SIGINT handler is
    never called again and
    i have to terminate the script with a SIGTERM or SIGKILL.
    >
    well, i would expect the handler to be called in both cases, am i
    missing something?
    >
    by the way. calling os.kill(os.getp id(), signal.SIGINT) works as I
    would expect, what
    don't is kill -s SIGINT pid # where pid is the actual process id
    >
    the code:
    >
    class Runner(object):
        def __init__(self):
            print os.getpid()
            self.shd = False
            signal.signal(s ignal.SIGINT, self.shutdown)
            threading.Threa d(target=self.d oSomething).sta rt()
            # the following works fine:
            #os.kill(os.get pid(), signal.SIGINT)
    >
        def doSomething(sel f):
            while not self.shd:
                pass
    >
        def shutdown(self, signo, frm):
            self.shd = True
    >
    if __name__ == '__main__':
        Runner()
    >
    ~levon

    Comment

    Working...