简体   繁体   中英

Should I always use threading.Thread.join()

I learned multithreading from here , and I use the last example as my template for all multithreading applications. Here's the code:

#!/usr/bin/python

import Queue
import threading
import time

exitFlag = 0

class myThread (threading.Thread):
    def __init__(self, threadID, name, q):
        threading.Thread.__init__(self)
        self.threadID = threadID
        self.name = name
        self.q = q
    def run(self):
        print "Starting " + self.name
        process_data(self.name, self.q)
        print "Exiting " + self.name

def process_data(threadName, q):
    while not exitFlag:
        queueLock.acquire()
        if not workQueue.empty():
            data = q.get()
            queueLock.release()
            print "%s processing %s" % (threadName, data)
        else:
            queueLock.release()
        time.sleep(1)

threadList = ["Thread-1", "Thread-2", "Thread-3"]
nameList = ["One", "Two", "Three", "Four", "Five"]
queueLock = threading.Lock()
workQueue = Queue.Queue(10)
threads = []
threadID = 1

# Create new threads
for tName in threadList:
    thread = myThread(threadID, tName, workQueue)
    thread.start()
    threads.append(thread)
    threadID += 1

# Fill the queue
queueLock.acquire()
for word in nameList:
    workQueue.put(word)
queueLock.release()

# Wait for queue to empty
while not workQueue.empty():
    pass

# Notify threads it's time to exit
exitFlag = 1

#here is where i do my write to file, the last operation**

# Wait for all threads to complete
for t in threads:
    t.join()
#print "Exiting Main Thread"

As you can see, this example uses t.join() on all of the threads to wait for them to complete. My actual version of this is super long and a large project, but that's not the point. I'm just wondering if there are any special cases when join() doesn't need to be used?

I'm just wondering if there are any special cases when join() doesn't need to be used?

The obvious case is when some other thread (which you're joining) is joining the thread, so you don't have to, but presumably you didn't need to be told about that one. :)

First, daemon threads do not need to be joined (and usually shouldn't be). You can just abandon them, and they'll be terminated abruptly at some point while your main thread is exiting. (Make sure not to do anything dangerous, like overwriting a file, in a daemon thread, of course.)

Second, if you don't join your (normal, non-daemon) threads, it's not actually documented exactly what will happen. Your main thread may wait for all of them in some arbitrary order, or it may exit and return to the shell while the threads keep working in the background, or it may kill them. But if you for some reason don't care at all which one of those happens, then don't join .

Third, on many platforms, it is well defined what will happen (and usually, it's "they all get join ed in some arbitrary order"). If, say, you're writing a program that only has to run on CPython under Red Hat Linux, and you're sure of what happens in CPython under Red Hat Linux, and your program structure makes it hard to keep track of your threads, then it's reasonable to just not join them (maybe with a comment) rather than rearchitecting your whole program.

Finally, dummy threads shouldn't be joined. But if you have dummy threads and didn't know you had dummy threads, you have bigger problems than join .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM