S
S
Space Around2020-10-08 21:43:17
Python
Space Around, 2020-10-08 21:43:17

Why in a bidirectional channel, the parent can only send data once?

The child process can send as much data as it likes, but the parent process can send it once, then the program stops and waits for something. The channel is open, it is not clear why this is happening

The code:

from multiprocessing import Process, Pipe, current_process
from multiprocessing.connection import wait

def foo(w, i):
    print("w -> 1234")
    w.send("1234")

    readers = []

    readers.append(w)

    while readers:
        for r in wait(readers):
            try:
                msg = r.recv()
            except EOFError:
                print("w: error")
                readers.remove(r)
            else:

                if(msg == "1234"):
                    w.send("4321")
                    print("1234 -> w -> 4321")

                if(msg == "4321"):
                    print("4321 -> w")
                    print("w: close")


if __name__ == '__main__':

    readers = []
    r, w = Pipe(duplex=True)
    readers.append(r)
    p = Process(target=foo, args=(w, 1))
    p.start()
    w.close()

    while readers:
        for r in wait(readers):
            try:
                msg = r.recv()
                if(msg == "1234"):
                    r.send("1234")
                    print("1234 -> r -> 1234")

                if(msg == "4321"):
                    r.send("r: 4321")
                    print("4321 -> r -> 4321")

            except EOFError:
                print("r: error")
                readers.remove(r)




The output is now:
w -> 1234
1234 -> r -> 1234
1234 -> w -> 4321
4321 -> r -> 4321


This is how it should be:
w -> 1234
1234 -> r -> 1234
1234 -> w -> 4321
4321 -> r -> 4321
4321 -> w
w: close

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question