Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to exit the entire application from a Python thread?

How can I exit my entire Python application from one of its threads? sys.exit() only terminates the thread in which it is called, so that is no help.

I would not like to use an os.kill() solution, as this isn't very clean.

like image 658
linkmaster03 Avatar asked Sep 28 '09 22:09

linkmaster03


People also ask

How do you exit a thread in Python?

In order to kill a thread, we use hidden function _stop() this function is not documented but might disappear in the next version of python.

How do you end a Python script?

Ctrl + C on Windows can be used to terminate Python scripts and Ctrl + Z on Unix will suspend (freeze) the execution of Python scripts. If you press CTRL + C while a script is running in the console, the script ends and raises an exception.

How do you start and stop a thread in Python?

You can't actually stop and then restart a thread since you can't call its start() method again after its run() method has terminated. However you can make one pause and then later resume its execution by using a threading. Condition variable to avoid concurrency problems when checking or changing its running state.

Does Python wait for threads to finish?

join() # Will wait for a thread until it finishes its task. You can also provide a timeout parameter in seconds (real numbers accepted) to the join() method.


2 Answers

Short answer: use os._exit.

Long answer with example:

I yanked and slightly modified a simple threading example from a tutorial on DevShed:

import threading, sys, os  theVar = 1  class MyThread ( threading.Thread ):     def run ( self ):        global theVar       print 'This is thread ' + str ( theVar ) + ' speaking.'       print 'Hello and good bye.'       theVar = theVar + 1       if theVar == 4:           #sys.exit(1)           os._exit(1)       print '(done)'  for x in xrange ( 7 ):    MyThread().start() 

If you keep sys.exit(1) commented out, the script will die after the third thread prints out. If you use sys.exit(1) and comment out os._exit(1), the third thread does not print (done), and the program runs through all seven threads.

os._exit "should normally only be used in the child process after a fork()" -- and a separate thread is close enough to that for your purpose. Also note that there are several enumerated values listed right after os._exit in that manual page, and you should prefer those as arguments to os._exit instead of simple numbers like I used in the example above.

like image 102
Mark Rushakoff Avatar answered Sep 21 '22 05:09

Mark Rushakoff


If all your threads except the main ones are daemons, the best approach is generally thread.interrupt_main() -- any thread can use it to raise a KeyboardInterrupt in the main thread, which can normally lead to reasonably clean exit from the main thread (including finalizers in the main thread getting called, etc).

Of course, if this results in some non-daemon thread keeping the whole process alive, you need to followup with os._exit as Mark recommends -- but I'd see that as the last resort (kind of like a kill -9;-) because it terminates things quite brusquely (finalizers not run, including try/finally blocks, with blocks, atexit functions, etc).

like image 20
Alex Martelli Avatar answered Sep 23 '22 05:09

Alex Martelli