Python - 如何以结束线程的方式结束函数(即将 threading.activeCount() 减少 1)



我刚刚开始尝试将线程作为一次下载多个文件的一种方式。 我的实现使用 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)

根据文档:

启动一个新线程并返回其标识符。线程执行 函数函数与参数列表参数(必须是元组)。 可选的 kwargs 参数指定关键字字典 参数。当函数返回时,线程以静默方式退出。什么时候 函数因未处理的异常而终止,堆栈跟踪为 打印,然后线程退出(但其他线程继续运行)。

(强调后加。

因此,当函数返回时,线程应该退出。

最新更新