I call to join , so i dont understand why the main thread print ‘End’ before all threaded were finished.
The output is :
End
Thread
Thread
Additional related question:
Just to make sure if is set daemon flag to true, is it mean that there is no meaning to call to join (because main can be finished when there are still daemon threads)?
#!/usr/bin/env python
import sys
import threading
from time import sleep
num_worker_threads = 2
threads = []
def worker():
sleep(1)
print 'Thread'
for i in range(num_worker_threads):
t = threading.Thread(target=worker)
threads.append(t)
t.start()
t.join
print 'End'
Because you actually don’t call join. Replace:
with:
The point is that
t.joinexpression returnsbound methodobject, and adding parentheses makes expression to be a method call.Joining threads matters only for user threads, daemon threads will not block main thread to finish.