Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

when will main thread exit in python

I am reading The Python Standard Library by Example and get confused when I arrived page 509.

Up to this point, the example programs have implicitly waited to exit until all threads have completed their work. Programs sometimes spawn a thread as a daemon that runs without blocking the main program from exiting.

but after I run some codes, I get result that is opposite. The code is like this:

    #!/usr/bin/env python
# encoding: utf-8
#
# Copyright (c) 2008 Doug Hellmann All rights reserved.
#
"""Creating and waiting for a thread.
"""
#end_pymotw_header

import threading
import time

def worker():
    """thread worker function"""
    print 'Worker'
    # time.sleep(10000)
    return

threads = []
for i in range(5):
    t = threading.Thread(target=worker)
    threads.append(t)
    t.start()

print "main Exit"

and sometime the result is this:

Worker
Worker
WorkerWorker

main Exit
Worker

So I want to ask when will main thread exit in python after it starts several thread?

like image 978
ssj Avatar asked Dec 03 '22 19:12

ssj


2 Answers

The main thread will exit whenever it is finished executing all the code in your script that is not started in a separate thread.

Since the t.start() will start the thread and then return to the main thread, the main thread will simply continue to execute until it reaches the bottom of your script and then exit.

Since you started the other threads in a non-daemon mode, they will continue running until they are finished.

If you wanted the main thread to not exit until all the threads have finished, you should explicitly join them to the main thread. The join will cause the thread calling join to wait until the the thread being joined is finished.

for i in range(5):
    threads[i].join()
print "main Exit"

As @codesparkle pointed out, the more Pythonic way to write this would be to skip the index variable entirely.

for thread in threads:
    thread.join()
print "main Exit"
like image 159
merlin2011 Avatar answered Jan 21 '23 18:01

merlin2011


According to the threading docs:

The entire Python program exits when only daemon threads are left

This agrees with the quote you give, but the slight difference in wording shows the result you get. The 'main thread' exits when you would expect it to. Note that the worker threads keep running at this point - you can see this in the test output you give. So, the main thread has finished, but the whole process is still running because there are other threads still running.

The difference is that if some of those worker threads were daemonised, they would be forcibly killed when the last non-daemon thread finished. If all of the workers were daemon threads, then the entire process would finish - and you would be back at your systems shell prompt - very soon after you print 'main exit', and it would be very rare (though not impossible, owing to race conditions) for any worker to print after that.

like image 27
lvc Avatar answered Jan 21 '23 17:01

lvc