为什么传递给我的worker函数的容器没有通过引用传递?

时间:2013-02-21 13:37:59

标签: python multithreading threadpool pass-by-reference

我的印象是python通过引用传递了对象,所以逻辑上(我认为),将字典传递给线程的工作函数将允许我设置新的键值对,这些对将在之后保留工人职能已经恢复。不幸的是,好像我错了!

不用多说,这是一个测试用例:

from Queue import Queue
from threading import Thread


def work_fn(dictionary, dfield):
    dictionary[dfield] = True


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(object):
    """Pool of threads consuming tasks from a queue"""
    def __init__(self, num_threads):
        self.tasks = Queue(num_threads)

        for i 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()


if __name__ == '__main__':
    pool = ThreadPool(4)

    data = [{} for _ in range(10)]
    for i, d in enumerate(data):
        pool.add_task(work_fn, d, str(i))

    pool.wait_completion()

    for d in data:
        print d.keys()

这里到底发生了什么?

我应该做些什么?

1 个答案:

答案 0 :(得分:0)

在打印结果之前,我没有看到您在等待完成任务的位置。在生成作业后,您似乎需要在后续循环中收集结果。