0

My early question is like this : How to give input multiple times to another program on python

My project is to make N time prisoner dilemma game, and I want to use two code made by C code to use in one python program.

For each one game, I want to get outputs from two programs, and give the result (If one tells the truth and another doesn't, one who tell will be innocent and another who doesn't will get 3 years prison life...something like that) to two programs, and repeat it N times.

So I made python code and C code like this

1. C Code

#include <stdio.h>
int n;
int result;
int main(){
    scanf("%d",&n);
    printf("1");
    for(int i=2;i<=n;i++){
        scanf("%d",&result);
        if(result==1) printf("1"); // 0 means another person remain silent, 1 means another person says truth.
        else printf("0");
    }
    return 0;
}

2. Python code

subprocess.call("/usr/bin/gcc -o p1 "+path,shell=True)
subprocess.call("/usr/bin/gcc -o p2 "+path,shell=True)
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(bytes('10\n','utf-8'))
cmd_2.stdin.write(bytes('10\n','utf-8'))
for i in range(10):
    cmd1_stdout, cmd1_stderr = cmd_1.communicate()
    cmd2_stdout, cmd2_stderr = cmd_2.communicate()
    for line in cmd1_stdout:
        print('result for p_1 : ' + chr(line))
        result_1 = line
    for line in cmd2_stdout:
        print('result for p_2 : ' + chr(line))
        result_2 = line
        if(i != 9):
            cmd_1.stdin.write(bytes(chr(result_2),'utf-8'))
        if(i != 9):
            cmd_2.stdin.write(bytes(chr(result_1),'utf-8'))

But this gives an error and the result is like this.

Error

ValueError: write to closed file

Result

result for p_1 : 1
result for p_1 : 0
result for p_1 : 0
result for p_1 : 0
result for p_1 : 0
result for p_1 : 0
result for p_1 : 0
result for p_1 : 0
result for p_1 : 0
result for p_1 : 0
result for p_2 : 1
result for p_2 : 0
result for p_2 : 0
result for p_2 : 0
result for p_2 : 0
result for p_2 : 0
result for p_2 : 0
result for p_2 : 0
result for p_2 : 0
result for p_2 : 0

I think that my C program close before I get cmd1_stdout, after call cmd_1.stdin.write().

But I want to make C program doesn't close before I give input N-1 times. How can I give multiple inputs to one program?

Community
  • 1
  • 1
  • I think you might need to send the input to the processes via the `input` keyword argument the [`Popen.communicate()`](https://docs.python.org/2/library/subprocess.html#subprocess.Popen.communicate) function accepts — **not** via the `stdin.write()` calls you're trying to use. – martineau May 09 '16 at 03:47
  • An alternate approach you could take is detailed in https://drive.google.com/file/d/0Bw5McUt95YdeMlNiX2VSR1lFRHM/view, namely - you could write all your code in Python and just annotate the CPU intensive hot-spots revealed by profiling. – boardrider May 09 '16 at 16:53

0 Answers0