safest way to kill a thread

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • martinnitram@excite.com

    #1

    safest way to kill a thread

    Dear all,
    in python, a thread can be created by t = threading.Threa d. But i
    found that when the main (and the thread) program is running and user
    use Crtl+C/Crtl+Z to break the program abnormally, the thread is still
    running and needed to kill manually (by the pid). Is there had any
    safest way to kill/exit the thread program under python (when the
    thread program part is a forever loop)?

    Thank a lot

  • limodou

    #2
    Re: safest way to kill a thread

    Using Thread's method setDaemon() before you call the start() method.
    Just like :

    t.setDaemon(Tru e)
    t.start()

    martinnitram@ex cite.com wrote:[color=blue]
    > Dear all,
    > in python, a thread can be created by t = threading.Threa d. But i
    > found that when the main (and the thread) program is running and user
    > use Crtl+C/Crtl+Z to break the program abnormally, the thread is still
    > running and needed to kill manually (by the pid). Is there had any
    > safest way to kill/exit the thread program under python (when the
    > thread program part is a forever loop)?
    >
    > Thank a lot
    >[/color]

    --
    I love python!
    My Blog: http://www.donews.net/limodou

    Comment

    • martinnitram@excite.com

      #3
      Re: safest way to kill a thread

      limodou wrote:[color=blue]
      >Using Thread's method setDaemon() before you call the start() method.
      >Just like :
      >t.setDaemon(Tr ue)
      >t.start()[/color]
      thank for fast reply.
      from python.org doc, said that setDaemon() function as
      "The entire Python program exits when no active non-daemon threads
      are left."
      is it mean that when the main program exit (normally/abnormally), all
      threads created will also exit?
      Thank again.

      Comment

      • limodou

        #4
        Re: safest way to kill a thread

        I think only those threads which invoked with setDaemon() method will
        exit, and others will not, as the main program exit.

        martinnitram@ex cite.com wrote:[color=blue]
        > limodou wrote:
        >[color=green]
        >>Using Thread's method setDaemon() before you call the start() method.
        >>Just like :
        >>t.setDaemon(T rue)
        >>t.start()[/color]
        >
        > thank for fast reply.
        > from python.org doc, said that setDaemon() function as
        > "The entire Python program exits when no active non-daemon threads
        > are left."
        > is it mean that when the main program exit (normally/abnormally), all
        > threads created will also exit?
        > Thank again.
        >[/color]

        --
        I love python!
        My Blog: http://www.donews.net/limodou

        Comment

        • martinnitram@excite.com

          #5
          Re: safest way to kill a thread

          Great thank for your helping.
          Should the 'daemonic' flag at setDaemon() function set to 1/TRUE or
          0/FALSE to do such action?

          limodou wrote:[color=blue]
          >I think only those threads which invoked with setDaemon() method will
          >exit, and others will not, as the main program exit.[/color]

          Comment

          • hoxide

            #6
            Re: safest way to kill a thread

            To Catch the "SystemExit "

            import thread
            from time import sleep
            import sys

            def t1():
            try:
            i=0
            while 1:
            print i+1
            i += 1
            sleep(1)
            except SystemExit:
            pass

            thread.start_ne w_thread(t1, ())
            sleep(3)


            This Question was also asked in Python-chinese .

            Comment

            • Peter Hansen

              #7
              Re: safest way to kill a thread

              martinnitram@ex cite.com wrote:[color=blue]
              > Should the 'daemonic' flag at setDaemon() function set to 1/TRUE or
              > 0/FALSE to do such action?[/color]

              First of all, it's "True" and "False" in Python, not TRUE
              and FALSE.

              Secondly, the answer to the question was in the previous
              message where "limodou" told you about this in the first
              place. Go back and read it again...

              -Peter

              Comment

              • martinnitram@excite.com

                #8
                Re: safest way to kill a thread

                Thank for all helping and sorry that i overlooked the previous
                message.

                Peter Hansen wrote:[color=blue]
                > martinnitram@ex cite.com wrote:[color=green]
                > > Should the 'daemonic' flag at setDaemon() function set to 1/TRUE or
                > > 0/FALSE to do such action?[/color]
                >
                > First of all, it's "True" and "False" in Python, not TRUE
                > and FALSE.
                >
                > Secondly, the answer to the question was in the previous
                > message where "limodou" told you about this in the first
                > place. Go back and read it again...
                >
                > -Peter[/color]

                Comment

                Working...