python - 如何在 Python 中的父进程和 fork 子进程之间共享数据?

标签 python fork share

我很确定有人会使用 os.plock(op) 函数来执行此操作,但我不知道如何操作。另外,如果有更好的方法,我将不胜感激。代码片段非常受欢迎。

最佳答案

Subprocess替换 os.popen、os.system、os.spawn、popen2 和命令。 simple example for piping会是:

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

你也可以使用 memory mapped file flag=MAP_SHARED 用于进程间的共享内存。

multiprocessing摘要 pipesshared memory并提供更高层次的接口(interface)。摘自处理文档:

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()

关于python - 如何在 Python 中的父进程和 fork 子进程之间共享数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/218935/

相关文章:

python - 即使在主线程崩溃后子线程仍继续运行

python - 多索引切片(涉及时间序列/日期范围)不适用于 DataFrame 但适用于 Series

c - 如何知道fork()代码中的输出?

ios - 在 Facebook 上分享,手机上不显示文字。 ( swift )

share - 在 ASP.NET Web 窗体中,将两个页面之间共享的代码放在哪里?

python - 如何获取邻居节点?

python - 如何按给定比例将一个生成器随机分成两个生成器?

fork - 如何允许外部用户在gitlab中fork项目?

c - fork() 和 wait() 有两个子进程

facebook - 使用 Corona sdk 免费版与 friend 共享应用程序