是否有可能让一些子进程运行一些计算,然后将结果发送到主进程(例如更新PyQt ui),但进程仍在运行,一段时间后它们会发回数据并再次更新ui? 使用multiprocessing.queue,似乎数据只能在进程终止后发回。 所以我想知道这种情况是否可能。提前谢谢!
答案 0 :(得分:1)
我不知道你的意思"使用multiprocessing.queue,似乎数据只能在进程终止后发回#34;这正是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