您如何在Python的父进程和派生子进程之间共享数据?

您如何在Python的父进程和派生子进程之间共享数据?,第1张

您如何在Python的父进程和派生子进程之间共享数据?

子进程替换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]

您还可以将带有标志=
MAP_SHARED的内存映射文件用于进程之间的共享内存。

多重处理可以抽象化管道和共享内存,并提供更高级别的接口。取自处理文档:

from multiprocessing import Process, Pipedef 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()


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5617217.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-15
下一篇 2022-12-15

发表评论

登录后才能评论

评论列表(0条)

保存