[英]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.