safest way to kill a thread

Discussion in 'Python' started by martinnitram@excite.com, Jan 19, 2005.

  1. Guest

    Dear all,
    in python, a thread can be created by t = threading.Thread. 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
    , Jan 19, 2005
    #1
    1. Advertising

  2. limodou Guest

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

    t.setDaemon(True)
    t.start()

    wrote:
    > Dear all,
    > in python, a thread can be created by t = threading.Thread. 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
    >


    --
    I love python!
    My Blog: http://www.donews.net/limodou
    limodou, Jan 19, 2005
    #2
    1. Advertising

  3. Guest

    limodou wrote:
    >Using Thread's method setDaemon() before you call the start() method.
    >Just like :
    >t.setDaemon(True)
    >t.start()

    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.
    , Jan 19, 2005
    #3
  4. limodou Guest

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

    wrote:
    > limodou wrote:
    >
    >>Using Thread's method setDaemon() before you call the start() method.
    >>Just like :
    >>t.setDaemon(True)
    >>t.start()

    >
    > 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.
    >


    --
    I love python!
    My Blog: http://www.donews.net/limodou
    limodou, Jan 19, 2005
    #4
  5. Guest

    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:
    >I think only those threads which invoked with setDaemon() method will
    >exit, and others will not, as the main program exit.
    , Jan 19, 2005
    #5
  6. hoxide Guest

    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_new_thread(t1, ())
    sleep(3)


    This Question was also asked in Python-chinese .
    hoxide, Jan 19, 2005
    #6
  7. Peter Hansen Guest

    wrote:
    > Should the 'daemonic' flag at setDaemon() function set to 1/TRUE or
    > 0/FALSE to do such action?


    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
    Peter Hansen, Jan 19, 2005
    #7
  8. Guest

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

    Peter Hansen wrote:
    > wrote:
    > > Should the 'daemonic' flag at setDaemon() function set to 1/TRUE or
    > > 0/FALSE to do such action?

    >
    > 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
    , Jan 20, 2005
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. rbt
    Replies:
    3
    Views:
    347
    Fredrik Lundh
    Feb 14, 2005
  2. Joseph Turian
    Replies:
    2
    Views:
    312
    Daniel Bickett
    Jul 27, 2005
  3. H5N1
    Replies:
    0
    Views:
    618
  4. Aussie Rules

    safest way to pass parameters between pages

    Aussie Rules, Oct 9, 2007, in forum: ASP .Net
    Replies:
    3
    Views:
    561
    Steven Cheng[MSFT]
    Oct 9, 2007
  5. J. Shane Kunkle

    Safest way to pass data between web apps?

    J. Shane Kunkle, Mar 11, 2005, in forum: ASP .Net Security
    Replies:
    4
    Views:
    453
    Paul Glavich [MVP ASP.NET]
    Mar 14, 2005
Loading...

Share This Page