Sending and receiving async over multiprocessing.Pipe() in Python

Posted by dcolish on Stack Overflow See other posts from Stack Overflow or by dcolish
Published on 2010-05-14T01:21:06Z Indexed on 2010/05/14 1:24 UTC
Read the original article Hit count: 414

Filed under:
|
|

I'm having some issues getting the Pipe.send to work in this code. What I would ultimately like to do is send and receive messages to and from the foreign process while its running in a fork. This is eventually going to be integrated into a pexpect loop for talking to interpreter processes.

` from multiprocessing import Process, Pipe

def f(conn):
    cmd = ''
    if conn.poll():
        cmd = conn.recv()
    i = 1
    i += 1
    conn.send([42 + i, cmd, 'hello'])

if __name__ == '__main__':
    parent_conn, child_conn = Pipe()
    p = Process(target=f, args=(child_conn,))
    p.start()
    from pdb import set_trace; set_trace()
    while parent_conn.poll():
        print parent_conn.recv()   # prints "[42, None, 'hello']"
        parent_conn.send('OHHAI')
    p.join()
`

© Stack Overflow or respective owner

Related posts about python

Related posts about multiprocessing