Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/visual-studio/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
未对Python线程进行垃圾收集_Python_Multithreading_Python Multithreading - Fatal编程技术网

未对Python线程进行垃圾收集

未对Python线程进行垃圾收集,python,multithreading,python-multithreading,Python,Multithreading,Python Multithreading,这是我的线程设置。在我的机器上,最大线程数是2047 class Worker(Thread): """Thread executing tasks from a given tasks queue""" def __init__(self, tasks): Thread.__init__(self) self.tasks = tasks self.daemon = True self.start() de

这是我的线程设置。在我的机器上,最大线程数是2047

class Worker(Thread):
    """Thread executing tasks from a given tasks queue"""
    def __init__(self, tasks):
        Thread.__init__(self)
        self.tasks = tasks
        self.daemon = True
        self.start()

    def run(self):
        while True:
            func, args, kargs = self.tasks.get()
            try:
                func(*args, **kargs)
            except Exception, e:
                print e
            self.tasks.task_done()

class ThreadPool:
    """Pool of threads consuming tasks from a queue"""
    def __init__(self, num_threads):
        self.tasks = Queue(num_threads)
        for _ in range(num_threads):
            Worker(self.tasks)

    def add_task(self, func, *args, **kargs):
        """Add a task to the queue"""
        self.tasks.put((func, args, kargs))

    def wait_completion(self):
        """Wait for completion of all the tasks in the queue"""
        self.tasks.join()
在我模块中的其他类中,我从上到下调用ThreadPool类 创建一个新的线程池。然后我执行操作。以下是一个例子:

def upload_images(self):
    '''batch uploads images to s3 via multi-threading'''
    num_threads = min(500, len(pictures))
    pool = ThreadPool(num_threads)

    for p in pictures:
        pool.add_task(p.get_set_upload_img)

    pool.wait_completion()
我遇到的问题是这些线程没有被垃圾收集

运行几次后,我的错误如下:

文件“/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/threading.py”,开始部分第495行 _启动新线程(self.\uu引导,()) thread.error:无法启动新线程

这意味着我已经达到了2047的线程限制


有什么想法吗?谢谢。

您的工作线程永远不会从运行中返回,因此您的线程永远不会结束

对于您的
run
方法,可能类似于以下内容

def run(self):
    while True:
        try:
            func, args, kargs = self.tasks.get()
        except Queue.Empty:
            break

        try:
            func(*args, **kargs)
        except Exception, e:
            print e

        self.tasks.task_done()

它看起来像一个无限循环,这可能是原因吗?所有线程都处于活动状态,因此无法通过gc收集它们。

我会尝试一下代码。认为tasks.task_done()在退出和垃圾收集线程的过程中做了一些事情!哦,@LucasOu-Yang
task\u-done()
应该像您一样位于异常处理程序之外(我会纠正它)。但是您仍然需要退出
run
方法。任务队列对处理线程一无所知。
def run(self):
    while True:
        func, args, kargs = self.tasks.get()
        try:
            func(*args, **kargs)
        except Exception, e:
            print e
        self.tasks.task_done()