How to give input multiple times to another program on python

前端 未结 2 649
死守一世寂寞
死守一世寂寞 2021-01-26 04:55

I want to make python file which opens two programs. This two programs have to get input from each other multiple times. I opened two programs and know how to give input to one

相关标签:
2条回答
  • 2021-01-26 05:12

    To exchange messages via pipes dynamically (not tested):

    #!/usr/bin/env python3
    from subprocess import Popen, PIPE
    
    with Popen('p1', stdin=PIPE, stdout=PIPE, universal_newlines=True) as p1, \
         Popen('p2', stdin=PIPE, stdout=PIPE, universal_newlines=True) as p2:
        # give_input(n)
        print(n, file=p1.stdin, flush=True)
        print(n, file=p2.stdin, flush=True)
        for i in range(n):
            # t_1 = get_output(t_2) //give input t_2, and get output t_1
            print(p1.stdout.read(1), file=p2.stdin, flush=True)
            # t_2 = get_output(t_1) //give input t_1, and get output t_2
            print(p2.stdout.read(1), file=p1.stdin, flush=True)
    

    It assumes that the child processes expect a line as a request and return a single character as a response.

    To make it work:

    1. p1, p2 should disable their internal stdout buffering (callsetvbuf(stdout, NULL, _IONBF, 0); at the start of the program) or call fflush() after each printf(). Related: Python C program subprocess hangs at “for line in iter”
    2. Put the directory with p1, p2 into $PATH or pass a full path instead 'p1' into Popen().
    0 讨论(0)
  • 2021-01-26 05:18

    Try using write and communicate to send data to cmd_1 and cmd_2 and get the response, see https://docs.python.org/3/library/subprocess.html#popen-constructor.

    for i in range(n):
        cmd_1 = subprocess.Popen("./p1",shell = True,stdin = subprocess.PIPE,stdout = subprocess.PIPE,stderr = subprocess.PIPE)
        cmd_2 = subprocess.Popen("./p2",shell = True,stdin = subprocess.PIPE,stdout = subprocess.PIPE,stderr = subprocess.PIPE)
        cmd_1.stdin.write(cmd_input[n])
        cmd_2.stdin.write(cmd_input[n])
        cmd1_stdout, cmd1_stderr = cmd_1.communicate()
        cmd2_stdout, cmd2_stderr = cmd_2.communicate()
        for line in cmd1_stdout:
            print(line.decode('ascii'))
        for line in cmd2_stdout:
            print(line.decode('ascii'))
    
    0 讨论(0)
提交回复
热议问题