同新方法

通过队列方式通信

子进程中通过queue作为参数传递到函数中,在函数中通过q.put将数据发送给主进程,主进程通过 q.get()获取到数据

通过管道的方式通信

类似socket的conn

from multiprocessing import Process,Pipe

def f(conn):
    conn.send("this is a message from son")
    response = conn.recv()
    print("response",response)
    conn.close()
    print("q_ID2",id(conn))

if __name__ == "__main__":
    parent_conn,child_conn = Pipe() #双向管道

    print("q_ID1:",id(child_conn,))
    p = Process(target=f,args=(child_conn,))
    p.start()
    print(parent_conn.recv())
    parent_conn.send("this is a message from parent")
    p.join()
'''
q_ID1: 174486304
this is a message from son
response this is a message from parent
q_ID2 174683696
'''

通过manager的方式通信

posted on 2018-11-05 16:21  cherrydot  阅读(107)  评论(0编辑  收藏  举报