Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.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_Queue_Multiprocessing - Fatal编程技术网

用python编写面向对象的多线程作业\结果队列

用python编写面向对象的多线程作业\结果队列,python,multithreading,queue,multiprocessing,Python,Multithreading,Queue,Multiprocessing,这里潜伏了很久 我有一个线程控制器对象。此对象接收称为“检查”的其他对象。这些检查会拉入与其条件匹配的DB行。线程管理器轮询每一个检查(询问它的DB行或工作单元),然后将每一行与对该检查对象的引用一起排队。其思想是N多个线程将进入并从队列中拉出一个项目,并执行相应的Check的do_work方法。do_work方法将返回Pass\Fail,所有过程将排队等待进一步处理 主脚本(未显示)实例化检查并使用add\u check将其添加到线程管理器中,然后调用kick\u off\u work 到目前

这里潜伏了很久

我有一个线程控制器对象。此对象接收称为“检查”的其他对象。这些检查会拉入与其条件匹配的DB行。线程管理器轮询每一个检查(询问它的DB行或工作单元),然后将每一行与对该检查对象的引用一起排队。其思想是N多个线程将进入并从队列中拉出一个项目,并执行相应的Check的do_work方法。do_work方法将返回Pass\Fail,所有过程将排队等待进一步处理

主脚本(未显示)实例化检查并使用add\u check将其添加到线程管理器中,然后调用kick\u off\u work

到目前为止,我正在测试,它只是锁定:

  import Queue
  from threading import Thread

  class ThreadMan:

    def __init__(self, reporter):
            print "Initializing thread manager..."
            self.workQueue = Queue.Queue()
            self.resultQueue = Queue.Queue()
            self.checks = []

    def add_check(self, check):
            self.checks.append(check)

    def kick_off_work(self):
            for check in self.checks:
                    for work_unit in check.populate_work():
                            #work unit is a DB row
                            self.workQueue.put({"object" : check, "work" : work_unit})


            threads = Thread(target=self.execute_work_unit)
            threads = Thread(target=self.execute_work_unit)
            threads.start()
            self.workQueue.join();

    def execute_work_unit(self):
            unit = self.workQueue.get()
            check_object = unit['object'] #Check object
            work_row = unit['work'] # DB ROW

            check_object.do_work(work_row)
            self.workQueue.task_done();
            print "Done with work!!"
其结果很简单:

Initializing thread manager...
In check1's do_work method... Doing work
Done with work!!

(locked up)
我想遍历整个队列

您应该只在执行工作单元中添加一个“while”,否则它会在第一次迭代时停止:

def execute_work_unit(self):
     while True:
        unit = self.workQueue.get()
        check_object = unit['object'] #Check object
        work_row = unit['work'] # DB ROW

        check_object.do_work(work_row)
        self.workQueue.task_done();
        print "Done with work!!"
看看那里:

编辑:要完成它,只需在self.workQueue.join()之后添加threads.join()
def开始工作(自我):

非常感谢。所有的工作现在都执行了,但最后仍然挂起。挂起是什么意思?(我不是英语母语)它还没有完成执行。