Problem with threads and signals

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Sebastian Meyer

    Problem with threads and signals

    Hi Newsgroup,

    i have some problems with using threads and signals in one program.
    In my program i have three threads running, one for checking a directory
    at a specified interval to see if new data arrived, one waiting for work
    and the main thread.

    My problem is the following:
    I want to shutdown the program via a signal, so i used the signal module.
    I know, that signal handling mus be done by the main thread, the signal
    handler is installed by the main thread. Now, when both threads are
    running and i send the signal to the main thread at my xterm via
    kill -SIGUSR1 PID, it seems that the main program ignores it or does
    not execute the signal handler. When i dont use threads, the signal
    handler functions properly.

    I ll post some code, maybe this is more explainig:

    --- CODE ---
    import threading
    import os,signal

    # *** signal handler ***
    def shutdown(signum , arg):
    global threads

    for th in threads:
    # the shutdown method sets an internal
    # flag in the derived thread class to
    # notify the thread to shutdown
    # (implemented using threading.Event )
    th.shutdown()

    # *** main ***

    # install the signal handler
    signal.signal(s ignal.SIGUSR1, shutdown)

    # both classes, TimedDirectoryC heck and BatchProcessor are derived from
    # threading.Threa d
    threads = {}
    threads["dircheck"] = TimedDirectoryC heck("/tmp/dpf/notify")
    threads["process"] = BatchProcessor( )

    # start the threads
    for th in threads:
    th.start()

    --- CODE ---

    thats is ... now both threads are running and doing their work. When
    i now want to shutdown and send the signal, nothing happens, even
    the signal handler will not be called?

    Have i missed something? I ve red a lot about threads and signals,
    seems that it sometimes causes strange program behavior...

    Anyone any idea what i did wrong?? Thanks for your help in advance..

    Sebastian

  • Diez B. Roggisch

    #2
    Re: Problem with threads and signals

    >[color=blue]
    > Anyone any idea what i did wrong?? Thanks for your help in advance..
    >
    > Sebastian[/color]

    Under Linux, similar things worked for me. Did you call setDaemon(True) on
    your bg threads?


    --
    Regards,

    Diez B. Roggisch

    Comment

    • Sebastian Meyer

      #3
      Re: Problem with threads and signals

      "Diez B. Roggisch" <deetsNOSPAM@we b.de> schrieb im Newsbeitrag
      news:c39d3f$nqm $01$2@news.t-online.com...
      [color=blue]
      > Under Linux, similar things worked for me. Did you call setDaemon(True) on
      > your bg threads?
      >[/color]
      No, i didnt call setDaemon ... is this essential to
      gain the functionality i want?

      Sebastian


      Comment

      • Jeff Epler

        #4
        Re: Problem with threads and signals

        The program below works fine on my system (redhat 9)

        A few months ago, debian-specific threading problems in Python were
        discussed. If you're using Debian, maybe it's relevant:


        Jeff

        import threading, os, signal, time

        _shutdown = 0

        class T(threading.Thr ead):
        def run(self):
        while not _shutdown:
        time.sleep(1)

        def shutdown(signum , arg):
        global _shutdown
        print "Shutting down..."
        _shutdown = 1

        def main():
        print "PID:", os.getpid()
        signal.signal(s ignal.SIGUSR1, shutdown)

        threads = [T(), T()]

        for th in threads:
        th.start()

        while not _shutdown:
        time.sleep(60)

        if __name__ == '__main__': main()

        Comment

        Working...