我很确定有人会使用 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摘要 pipes和 shared 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/