Terminating threaded programs

Discussion in 'Python' started by mk, Feb 5, 2010.

  1. mk

    mk Guest

    Hello everyone,

    I have a problem with a threaded program: it frequently hangs on sys.exit.

    The problem is that my program uses threads which in turn use paramiko
    library, which itself is threaded.

    I try to gracefully close the threads (below), but it doesn't always
    work, if paramiko calls happen to be at stage of negotiating ssh
    connection or smth similar.

    The only workable solution I have is a program sending itself SIGKILL,
    which makes it terminated by OS (I think so).

    Is there any way to brutally close the threads? I know that normally
    that should not be done, but shutdown when you don't care about writing
    out to disk is the only situation where it doesn't apply.

    def ctrlchandler(signal, frame):
    print
    print ENDC + "Terminating on Ctrl-C, closing threads for:",
    while queue:
    for ip, th in queue:
    print ip,
    try:
    lock.acquire()
    th.abort = True
    lock.release()
    except RuntimeError:
    pass
    queue.remove((ip,th))
    print
    pid = os.getpid()
    print "Finished closing threads."
    # suicide - it's the only way of preventing frequent hangup on sys.exit
    os.kill(pid, SIGTERM)
    os.kill(pid, SIGKILL)
    sys.exit(0)
    mk, Feb 5, 2010
    #1
    1. Advertising

  2. mk

    Aahz Guest

    In article <>,
    mk <> wrote:
    >
    >I have a problem with a threaded program: it frequently hangs on sys.exit.
    >
    >The problem is that my program uses threads which in turn use paramiko
    >library, which itself is threaded.
    >
    >I try to gracefully close the threads (below), but it doesn't always
    >work, if paramiko calls happen to be at stage of negotiating ssh
    >connection or smth similar.
    >
    >The only workable solution I have is a program sending itself SIGKILL,
    >which makes it terminated by OS (I think so).


    You can also use os._exit().
    --
    Aahz () <*> http://www.pythoncraft.com/

    "At Resolver we've found it useful to short-circuit any doubt and just
    refer to comments in code as 'lies'. :)"
    Aahz, Feb 11, 2010
    #2
    1. Advertising

  3. mk

    Guest

    On Feb 11, 2010, at 11:43 AM, Aahz wrote:

    > In article <>,
    > mk <> wrote:
    >>
    >> I have a problem with a threaded program: it frequently hangs on sys.exit.
    >>
    >> The problem is that my program uses threads which in turn use paramiko
    >> library, which itself is threaded.
    >>
    >> I try to gracefully close the threads (below), but it doesn't always
    >> work, if paramiko calls happen to be at stage of negotiating ssh
    >> connection or smth similar.
    >>
    >> The only workable solution I have is a program sending itself SIGKILL,
    >> which makes it terminated by OS (I think so).

    >
    > You can also use os._exit().


    This just came up on the Twisted IRC channel last night and came to the same conclusion. Python's going to wait for threads to terminate and if they're not going to, neither is Python. os._exit() came up as the 'last resort' way out of the app.

    S
    , Feb 11, 2010
    #3
  4. mk

    mk Guest

    Aahz wrote:

    > You can also use os._exit().


    Thanks!
    mk, Feb 11, 2010
    #4
  5. mk

    mk Guest

    Aahz wrote:

    > You can also use os._exit().


    Yes! It works cleanly! Thanks a million!

    OTOH, if I use sys.exit(), it's just hanging there.

    Regards,
    mk
    mk, Feb 11, 2010
    #5
    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. Rodney Edmondson
    Replies:
    11
    Views:
    644
    Jeremy
    Dec 9, 2003
  2. Replies:
    12
    Views:
    1,618
    Dave Thompson
    Jan 10, 2005
  3. JLeidel

    time in threaded programs

    JLeidel, Jan 28, 2008, in forum: C Programming
    Replies:
    13
    Views:
    512
    Keith Thompson
    Jan 30, 2008
  4. Virchanza
    Replies:
    8
    Views:
    1,086
    Nobody
    Apr 6, 2011
  5. Replies:
    1
    Views:
    110
    Joost Diepenmaat
    Apr 13, 2008
Loading...

Share This Page