multiprocessing - child process constantly sending back results and keeps running

允我心安 提交于 2020-07-24 04:12:49

问题


Is it possible to have a few child processes running some calculations, then send the result to main process (e.g. update PyQt ui), but the processes are still running, after a while they send back data and update ui again? With multiprocessing.queue, it seems like the data can only be sent back after process is terminated. So I wonder whether this case is possible or not.


回答1:


I don't know what you mean by "With multiprocessing.queue, it seems like the data can only be sent back after process is terminated". This is exactly the use case that Multiprocessing.Queue was designed for.

PyMOTW is a great resource for a whole load of Python modules, including Multiprocessing. Check it out here: https://pymotw.com/2/multiprocessing/communication.html

A simple example of how to send ongoing messages from a child to the parent using multiprocessing and loops:

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


来源:https://stackoverflow.com/questions/46036577/multiprocessing-child-process-constantly-sending-back-results-and-keeps-runnin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!