from subprocess import Popen, PIPE, STDOUT from sys import argv, stdin, stdout, executable def writeRead(process, text): data = text.encode("ASCII") print(">> write: %s" % repr(data)) stdout.flush() process.stdin.write(data) process.stdin.flush() line = process.stdout.readline() line = line.rstrip() print("<< read: %s" % line) stdout.flush() def parent(): process = Popen([executable, "-u", __file__, "child"], stdin=PIPE, stdout=PIPE, stderr=STDOUT) writeRead(process, "aaa") writeRead(process, "bb\n") print("Close"); stdout.flush() stdout.flush() process.stdin.close() print("Wait"); stdout.flush() process.wait() print("Exit code: %s" % process.returncode) def child(): while True: data = stdin.read(3) print("data: [%s] %s" % (len(data), repr(data))) if not data: break if __name__ == "__main__": if len(argv) == 2 and argv[1] == "child": child() else: parent()