简体   繁体   English

多处理:如何将数据从父进程发送到持续运行的子进程?

[英]multiprocessing: how to send data from parent to continuously running child process?

Is it possible to use multiprocessing to send data to a running child process?是否可以使用多处理将数据发送到正在运行的子进程? For example, running a web server in a child process and using the parent process to send data to the server.例如,在子进程中运行 web 服务器,并使用父进程向服务器发送数据。 I found this question about the exact reverse of my situation (ie continuous updates from a running child process back to the parent).我发现这个问题与我的情况完全相反(即从正在运行的子进程不断更新回父进程)。 Is there a way to get this to work?有没有办法让它工作? My understanding is that multiprocessing.Queue only sends/receives data on process termination.我的理解是 multiprocessing.Queue 仅在进程终止时发送/接收数据。 This pseudocode gives a skeleton of what I'd like to do:这个伪代码给出了我想做的事情的骨架:

def server(q):
    //start eventlet server
    q.get() // --> does something

q = Queue()
p = Process(target=server, args=(q,))
p.start()
q.put("some kind of command")

I think you misread the answer you quoted.我认为您误读了您引用的答案。 The multiprocessing.Queue was designed for exactly the purpose you describe. multiprocessing.Queue专为您描述的目的而设计。 Maybe check out the reference documentation for that class?也许查看 class 的参考文档? An alternative may also be multiprocessing.Pipe .另一种选择也可能是multiprocessing.Pipe

Your pseudocode runs almost exactly as written:您的伪代码几乎完全按照编写的方式运行:

from multiprocessing import Queue, Process
import time

def server(q):
    while True:
        stuff = q.get()
        print(stuff, flush=True)

if __name__ == '__main__':
    q = Queue()
    p = Process(target=server, args=(q,))
    p.start()
    for i in range(5):
        q.put(i)
        time.sleep(1)
    p.terminate()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 python multiprocessing - 将子进程日志发送到在父进程中运行的 GUI - python multiprocessing - sending child process logging to GUI running in parent 如何在 NodeJS 中使用 Spawn 将数据从子进程发送回父进程 - How to send data from child process back to parent using Spawn in NodeJS 无法从多处理中的 prom 父进程获取另一个子进程中的数据 python - Failed to get data in another child from prom parent process in multiprocessing python python多处理将子进程中的di​​ct传递回父进程 - python multiprocessing pass dict from child process back to parent python multiprocessing - 子进程阻塞父进程 - python multiprocessing - child process blocking parent process 如何使用 Python 和 Windows 中的多处理从子进程中杀死父进程? - How can I kill a parent from a child process using multiprocessing in Python and Windows? Python 多处理,子进程在发送前引发异常后,父进程在接收时挂起 - Python multiprocessing, parent process hangs at recv after child process raises exception before send 如何使用多重处理将对象从主流程传递到子流程 - How to pass object from main process to child process using multiprocessing 如何从服务器连续向浏览器发送数据 - How to send data continuously from server to browser 如何终止多处理父流程? - How to Terminate Multiprocessing Parent Process?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM