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

后端 未结 5 2033
北恋
北恋 2020-12-10 13:23

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 v

5条回答
  •  被撕碎了的回忆
    2020-12-10 13:57

    Subprocess replaces os.popen, os.system, os.spawn, popen2 and commands. A simple example for piping would be:

    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 file with the flag=MAP_SHARED for shared memory between processes.

    multiprocessing abstracts both pipes and shared memory and provides a higher level interface. Taken from the Processing documentation:

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

提交回复
热议问题