【问题标题】:Python multiprocessing without blocking parent processPython多处理而不阻塞父进程
【发布时间】:2021-05-01 08:16:09
【问题描述】:

我正在尝试创建一个简单的应用程序,它会持续监控收件箱,然后在对传入邮件进行分类后,将各种函数作为子进程调用。

我希望父进程在不等待子进程完成的情况下继续它的 while 循环。 例如:

def main():
    while 1:
        checkForMail()
        if mail:
            if mail['type'] = 'Type1':
                process1() # 
                '''
                spawn process1, as long as no other process1 process running,
                however it's fine for a process2 to be currently running
                '''
            elif mail['type'] = 'Type2':
                process2()
                '''
                spawn process2, as long as no other process2 process running,
                however it's fine for a process1 to be currently running
                '''

        # Wait a bit, then continue loop regardless of whether child processes have finished or not
        time.sleep(10)
if __name__ == '__main__':
    main()

如上所述,一个函数的并发子进程实例不应超过一次,但是如果进程运行不同的函数,它们可以同时运行。

这可能与多处理包有关吗?

【问题讨论】:

    标签: python multiprocessing


    【解决方案1】:

    根据 pdeubel 的回答非常有帮助,完成的骨架脚本如下:

    所以在主循环之前启动两个进程,然后启动主循环,邮件应该放在队列中,然后在子进程中接收。

    def func1(todo):
        # do stuff with current todo item from queue1
    
    def func2(todo):
        # do stuff with current todo item from queue2
    
    def listenQ1(q):
        while 1:
            # Fetch jobs from queue1
            todo = q.get()
            func1(todo)
    def listenQ2(q):
        while 1:
            # Fetch jobs from queue2
            todo = q.get()
            func2(todo)
    
    def main(queue1, queue2):
        while 1:
            checkForMail()
            if mail:
                if mail['type'] = 'Type1':
                    # Add to queue1
                    queue1.put('do q1 stuff')
    
                elif mail['type'] = 'Type2':
                    # Add job to queue2
                    queue2.put('do q2 stuff')
        time.sleep(10)
    
    if __name__ == '__main__':
        # Create 2 multiprocessing queues
        queue1 = Queue()
        queue2 = Queue()
    
        # Create and start two new processes, with seperate targets and queues
        p1 = Process(target=listenQ1, args=(queue1,))
        p1.start()
        p2 = Process(target=listenQ2, args=(queue2,))
        p2.start()
    
        # Start main while loop and check for mail
        main(queue1, queue2)
    
        p1.join()
        p2.join()
    

    【讨论】:

      【解决方案2】:

      您可以使用两个Queues,一个用于Type1 邮件,一个用于Type2 邮件,两个Processes 再次用于Type1 邮件,一个用于Type2 邮件。

      首先创建这些队列。然后创建进程并将第一个队列分配给第一个进程,将第二个队列分配给第二个进程。两个 Process 对象都需要一个参数 target,这是 Process 执行的函数。根据逻辑,您可能需要两个函数(每种类型又一个)。在函数内部,您想要一个类似于无限循环的东西,它从队列中获取项目(即邮件),然后根据您的逻辑对它们进行操作。主函数还包含一个无限循环,在该循环中检索邮件并根据它们的类型将它们放置在正确的队列中。

      所以在主循环之前启动两个进程,然后启动主循环,邮件应该放在队列中,然后在子进程中接收它们。

      【讨论】:

        猜你喜欢
        • 2017-08-29
        • 2019-04-06
        • 2014-05-25
        • 2014-07-01
        • 1970-01-01
        • 2022-01-21
        • 2011-12-30
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多