添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

不阻塞父进程的Python多进程

0 人关注

我试图创建一个简单的应用程序,持续监控收件箱,然后在对收到的邮件进行分类后,作为子进程调用各种功能。

我想让父进程继续它的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
R3uben
R3uben
发布于 2021-01-27
2 个回答
R3uben
R3uben
发布于 2021-01-27
已采纳
0 人赞同

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