简体   繁体   中英

Python - How to end function in a way that ends thread (i.e. decrease threading.activeCount() by 1)?

I've just starting experimenting with threading as a way to download multiple files at once. My implementation uses thread.start_new_thread().

I want to download 10 files at a time, then wait for all 10 files to finish downloading before starting the next 10 files. In my code below, threading.activeCount() never decreases, even when download() ends with exit(), sys.exit() or return.

My workaround was to introduce the downloadsRemaining counter, but now the number of active threads continually increases. At the end of the sample program below, there will be 500 active threads, where I really only want 10 at a time.

import urllib
import thread
import threading
import sys

def download(source, destination):

    global threadlock, downloadsRemaining

    audioSource = urllib.urlopen(source)
    output = open(destination, "wb")
    output.write(audioSource.read())
    audioSource.close()
    output.close()

    threadlock.acquire()
    downloadsRemaining = downloadsRemaining - 1
    threadlock.release()

    #exit()
    #sys.exit()    None of these 3 commands decreases threading.activeCount()
    #return


for i in range(50):
    downloadsRemaining = 10
    threadlock = thread.allocate_lock()

    for j in range(10):
        thread.start_new_thread(download, (sourceList[i][j], destinationList[i][j]))

    #while threading.activeCount() > 0:  <<<I really want to use this line rather than the next
    while downloadsRemaining > 0:
        print "NUMBER ACTIVE THREADS:  " + str(threading.activeCount())
        time.sleep(1)

According to the documentation :

Start a new thread and return its identifier. The thread executes the function function with the argument list args (which must be a tuple). The optional kwargs argument specifies a dictionary of keyword arguments. When the function returns, the thread silently exits. When the function terminates with an unhandled exception, a stack trace is printed and then the thread exits (but other threads continue to run).

(Emphasis added.)

So the thread should exit when the function returns.

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