多处理 - 子进程不断发回结果并保持 运行
multiprocessing - child process constantly sending back results and keeps running
是否可以让几个子进程运行进行一些计算,然后将结果发送给主进程(例如更新PyQt ui),但进程仍然运行 ,过了一会儿他们发回数据并再次更新 ui?
使用 multiprocessing.queue,似乎只能在进程终止后才能将数据发回。
所以我想知道这种情况是否可能。
我不知道你所说的 "With multiprocessing.queue, it seems like the data can only be sent back after process is terminated" 是什么意思。这正是 Multiprocessing.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
是否可以让几个子进程运行进行一些计算,然后将结果发送给主进程(例如更新PyQt ui),但进程仍然运行 ,过了一会儿他们发回数据并再次更新 ui? 使用 multiprocessing.queue,似乎只能在进程终止后才能将数据发回。 所以我想知道这种情况是否可能。
我不知道你所说的 "With multiprocessing.queue, it seems like the data can only be sent back after process is terminated" 是什么意思。这正是 Multiprocessing.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