Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Terminating threaded programs

Reply
Thread Tools

Terminating threaded programs

 
 
mk
Guest
Posts: n/a
 
      02-05-2010
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)

 
Reply With Quote
 
 
 
 
Aahz
Guest
Posts: n/a
 
      02-11-2010
In article <(E-Mail Removed)>,
mk <(E-Mail Removed)> 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 ((E-Mail Removed)) <*> 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'. "
 
Reply With Quote
 
 
 
 
ssteinerX@gmail.com
Guest
Posts: n/a
 
      02-11-2010

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

> In article <(E-Mail Removed)>,
> mk <(E-Mail Removed)> 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

 
Reply With Quote
 
mk
Guest
Posts: n/a
 
      02-11-2010
Aahz wrote:

> You can also use os._exit().


Thanks!


 
Reply With Quote
 
mk
Guest
Posts: n/a
 
      02-11-2010
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Compiling multi-threaded programs with the GNU compiler suite Virchanza C++ 8 04-06-2011 01:00 AM
global variables in threaded perl programs iloveperl@abc.com Perl Misc 1 04-13-2008 12:28 AM
time in threaded programs JLeidel C Programming 13 01-30-2008 06:55 PM
Terminating a thread from the main thread Charles A. Lackman ASP .Net 3 12-09-2004 02:12 PM
Why do Java offers two different ways to write the multi-threaded Java programs? Rodney Edmondson Java 11 12-09-2003 03:46 AM



Advertisments