如何在 Python 中的父进程和分叉子进程之间共享数据?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/218935/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 19:39:43  来源:igfitidea点击:

How do you share data between a parent and forked child process in Python?

pythonforkshare

提问by Florian B?sch

I'm pretty sure one would do this using the os.plock(op) function, but I have no idea how. Also, if there's a better way, I'd be grateful to find out. Code snippets are very welcome.

我很确定有人会使用 os.plock(op) 函数来做到这一点,但我不知道怎么做。另外,如果有更好的方法,我将不胜感激。非常欢迎代码片段。

回答by Florian B?sch

Subprocessreplaces os.popen, os.system, os.spawn, popen2 and commands. A simple example for pipingwould be:

子进程替换了 os.popen、os.system、os.spawn、popen2 和命令。管道的一个简单示例是:

p1 = Popen(["dmesg"], stdout=PIPE)
p2 = Popen(["grep", "hda"], stdin=p1.stdout, stdout=PIPE)
output = p2.communicate()[0]

You could also use a memory mapped filewith the flag=MAP_SHARED for shared memory between processes.

您还可以使用带有 flag=MAP_SHARED的内存映射文件作为进程之间的共享内存。

multiprocessingabstracts both pipesand shared memoryand provides a higher level interface. Taken from the Processing documentation:

multiprocessing抽象了管道共享内存,并提供了更高级别的接口。取自处理文档:

from multiprocessing import Process, Pipe

def f(conn):
    conn.send([42, None, 'hello'])
    conn.close()

if __name__ == '__main__':
    parent_conn, child_conn = Pipe()
    p = Process(target=f, args=(child_conn,))
    p.start()
    print parent_conn.recv()   # prints "[42, None, 'hello']"
    p.join()

回答by Brian

Take a look at the multiprocessingmodule new in python 2.6 (also available for earlier versions a pyprocessing

看一下python 2.6 中新的multiprocessing模块(也可用于早期版本的pyprocessing

Here's an example from the docs illustrating passing information using a pipe for instance:

这是文档中的一个示例,说明使用管道传递信息,例如:

from multiprocessing import Process, Pipe

def f(conn):
    conn.send([42, None, 'hello'])
    conn.close()

if __name__ == '__main__':
    parent_conn, child_conn = Pipe()
    p = Process(target=f, args=(child_conn,))
    p.start()
    print parent_conn.recv()   # prints "[42, None, 'hello']"
    p.join()

回答by Dan Lenski

This is pretty much Python-independent! It's a classic example of Unix interprocess communication. One good option is to use popen()to open a pipe between the parent and child processes, and pass data/messages back and forth along the pipe.

这几乎与 Python 无关!它是 Unix 进程间通信的经典示例。一个不错的选择是使用popen()在父进程和子进程之间打开管道,并沿管道来回传递数据/消息。

Take a look at the subprocessmodule, which can set up the necessary pipes automatically while spawning child processes.

看看subprocess模块,它可以在生成子进程时自动设置必要的管道。

回答by mipadi

You have two options: os.popen*in the osmodule, or you can use the subprocessmodule to the same effect. The Python manual has pretty documentation and examples for popenand subprocess.

你有两个选择:os.popen*os模块中,或者你可以使用subprocess模块达到同样的效果。Python 手册中有关于popensubprocess 的漂亮文档和示例。

回答by David Fraser

If you are doing low-level operating system forking and really want to avoid using pipes, it is possible to use shared memory-mapped files as well. This is not nearly as nice as using subprocessor popenpipes, but including the answer for completeness...

如果您正在执行低级操作系统分叉并且真的想避免使用管道,那么也可以使用共享内存映射文件。这几乎不像使用subprocesspopen管道那么好,但包括完整性的答案......

There's a full example here, but basically you can combine the osfile handling and mmapmodules:

这里有一个完整的例子,但基本上你可以结合os文件处理和mmap模块:

import mmap, os, tempfile
fd, tmpfile = tempfile.mkstemp()
os.write(fd, '\x00' * mmap.PAGESIZE)
os.lseek(fd, 0, os.SEEK_SET)
child_pid = os.fork()
if child_pid:
    buf = mmap.mmap(fd, mmap.PAGESIZE, mmap.MAP_SHARED, mmap.PROT_READ)
    os.waitpid(child_pid, 0)
    child_message = buf.readline()
    print(child_message)
    os.close(fd)
else:
    buf = mmap.mmap(fd, mmap.PAGESIZE, mmap.MAP_SHARED, mmap.PROT_WRITE)
    buf.write('testing\n')
    os.close(fd)