我刚刚开始尝试使用线程作为一次下载多个文件的方式。我的实现使用thread.start_new_thread()。
我想一次下载 10 个文件,然后等待所有 10 个文件完成下载,然后再开始下 10 个文件。在我下面的代码中,threading.activeCount() 永远不会减少,即使 download() 以 exit()、sys.exit() 或 return 结束。
我的解决方法是引入 downloadsRemaining 计数器,但现在活动线程的数量不断增加。在下面的示例程序结束时,将有 500 个活动线程,我一次只需要 10 个。
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)