如何在python中杀死完成的线程

时间:2016-08-07 22:21:19

标签: python multithreading

我的多线程脚本引发了这个错误:

thread.error : can't start new thread

当它达到460个线程时:

threading.active_count() = 460

我认为旧线程保持堆叠,因为脚本没有杀死它们。这是我的代码:

import threading
import Queue
import time
import os
import csv


def main(worker):
    #Do Work
    print worker
    return

def threader():
    while True:
        worker = q.get()
        main(worker)
        q.task_done()        

def main_threader(workers):
    global q
    global city
    q = Queue.Queue()
    for x in range(20):
        t = threading.Thread(target=threader)
        t.daemon = True
        print "\n\nthreading.active_count() = "  + str(threading.active_count()) + "\n\n"
        t.start()
    for worker in workers:
        q.put(worker)   
    q.join()

如何在作业完成后杀死旧线程? (还不够吗?)

1 个答案:

答案 0 :(得分:0)

您的threader函数永不退出,因此您的线程永远不会消亡。由于您只是处理一组固定的工作并且在开始工作后从不添加项目,因此您可以将线程设置为在队列为空时退出。

请参阅以下修改后的代码版本以及我添加的评论:

def threader(q):
    # let the thread die when all work is done
    while not q.empty():
        worker = q.get()
        main(worker)
        q.task_done()        

def main_threader(workers):
    # you don't want global variables
    #global q
    #global city
    q = Queue.Queue()
    # make sure you fill the queue *before* starting the worker threads
    for worker in workers:
        q.put(worker)  
    for x in range(20):
        t = threading.Thread(target=threader, args=[q])
        t.daemon = True
        print "\n\nthreading.active_count() = "  + str(threading.active_count()) + "\n\n"
        t.start() 
    q.join()

请注意,我删除了global q,而是将q传递给了线程函数。您不希望先前调用创建的线程最终与新线程共享q编辑虽然q.join()无论如何都会阻止此操作,但最好还是避免使用全局线程)