简体   繁体   English

用于多种类型流程的多处理流程队列

[英]Multiprocessing process queues for multiple types of processes

I am relatively new to Python, and I am trying to create a queue which holds a number of different processes. 我对Python还是比较陌生,我正在尝试创建一个包含许多不同进程的队列。 There are a total of 3 processes called Process1, Process2, and Process3. 共有3个进程,分别称为Process1,Process2和Process3。 When Process1 completes its execution, I want a new process Process2 to be added to the queue. 当Process1完成执行时,我希望将新进程Process2添加到队列中。 When Process2 completes its execution, I want a new process Process3 to be added to the queue. 当Process2完成执行时,我希望将新进程Process3添加到队列中。

The reason why I am wanting to use a queue is because if Process2 fails, I want to move this task to the back of the queue so that it can be executed at a later time. 我要使用队列的原因是因为如果Process2失败,我想将此任务移到队列的后面,以便以后可以执行。

Here is my current implementation: 这是我当前的实现:

from multiprocessing import Process, Queue
import time

class Process1(Process):
    def __init__(self, queue):
            super(Process1, self).__init__()
            self.queue = queue

    def run(self):
            print 'I am Process 1'
            time.sleep(1)
            print 'Done process 1'
            p2 = Process2(self.queue)
            self.queue.put(p2)
            p2.start()
            p2.join()

class Process2(Process):
    def __init__(self, queue):
            super(Process2, self).__init__()
            self.queue = queue

    def run(self):
            print 'I am Process 2'
            time.sleep(2)
            print 'Done process 2'
            p3 = Process3(self.queue)
            self.queue.put(p3)
            p3.start()
            p3.join()

class Process3(Process):
    def __init__(self, queue):
            super(Process3, self).__init__()
            self.queue = queue

    def run(self):
            print 'I am Process 3'
            time.sleep(3)
            print 'Done process 3'


if __name__ == '__main__':
    queue = Queue()

    p1 = Process1(queue)
    p1.start()
    p1.join()

When this code is executed, I am getting this error: 执行此代码后,出现此错误:

RuntimeError: Queue objects should only be shared between processes through inheritance.

I am really having trouble understanding Python Processes and Queues. 我真的很难理解Python进程和队列。 I have tried reading Python's documentation, but I am having trouble finding a solution for what I am wanting to do. 我已经尝试阅读Python的文档,但是在寻找解决方案时遇到了麻烦。 If anyone could provide me with background reading that would be awesome. 如果有人可以为我提供背景阅读,那将是很棒的。 Thanks! 谢谢!

Multiprocessing queues are to be used by a processes immediate children only. 多进程队列仅由进程的直接子进程使用。 By having Process1 create Process2 and Process2 create Process3, you are trying to hand the queue to the process grandchildren and great-grandchildren. 通过让Process1创建Process2和Process2创建Process3,您正在尝试将队列传递给流程的孙代和曾孙代。 You need to have your top level process (the code in main ) decide when children need to be created and it should also hold the rultes for dealing with failed children and requeuing. 您需要让您的顶级流程( main中的代码)决定何时需要创建子代,并且还应具有处理失败子代和重新排队的规则。 Generally, the top level process will make decisions based on result objects when process work is done. 通常,完成流程工作时,顶级流程将根据结果对象做出决策。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM