Python多处理子进程不断地返回结果并保持运行

2024-04-20 03:06:18 发布

您现在位置:Python中文网/ 问答频道 /正文

有没有可能让几个子进程运行一些计算,然后将结果发送到主进程(例如更新PyQt ui),但是这些进程仍然在运行,过了一段时间,它们会发回数据并再次更新ui? 与多处理.queue,似乎只有进程终止后才能将数据发回。 所以我想知道这个案子是否可能。提前谢谢!在


Tags: 数据uiqueue进程pyqt案子
1条回答
网友
1楼 · 发布于 2024-04-20 03:06:18

我不知道你说的“和”是什么意思多处理.queue,似乎只有进程终止后才能将数据发回”。这正是多处理队列是为。在

PyMOTW对于整个Python模块(包括多处理)来说是一个很好的资源。看看这里:https://pymotw.com/2/multiprocessing/communication.html

如何使用简单循环将消息从父循环发送到子循环:

import multiprocessing

def child_process(q):
    for i in range(10):
        q.put(i)
    q.put("done")  # tell the parent process we've finished

def parent_process():
    q = multiprocessing.Queue()
    child = multiprocessing.Process(target=child_process, args=(q,))
    child.start()
    while True:
        value = q.get()
        if value == "done":  # no more values from child process
            break
        print value
        # do other stuff, child will continue to run in separate process

相关问题 更多 >