Hi guys,

I need your help. I am using python to synchronize several c programs. For one of these programs I need a dynamic interaction. I know there are several option: I am interested in the simplest one becouse I am noob.

Python code and C code have to exange data: python lunch the c program with some input, c code elaborate it and generate some results, these results have to be elaborated by the python code and new input will be delivered back to the c program.

I cannot use subprocess.communicate(input) becouse it will wait the end of the c process. The best it will be or a exchanging system through stdin and stdout or shared datastructure but I don't know how to deal with it in this case.

Could you help me?

Hi, yes I checked the Hellmann's tutorial and you are probably talking about this example:

import subprocess

print 'One line at a time:'
proc = subprocess.Popen('repeater.py',
                       shell=True,
                       stdin=subprocess.PIPE,
                       stdout=subprocess.PIPE,
                       )
for i in range(10):
   proc.stdin.write('%d\n' % i)
   output = proc.stdout.readline()
   print output.rstrip()
proc.communicate()

print
print 'All output at once:'
proc = subprocess.Popen('repeater.py',
                       shell=True,
                       stdin=subprocess.PIPE,
                       stdout=subprocess.PIPE,
                       )
for i in range(10):
   proc.stdin.write('%d\n' % i)

output = proc.communicate()[0]
print output

This example works fine with reaper.py but not with this simple c code:

#include <stdio.h>
int main(void)
{
    int n;
    while (scanf("%d", &n) == 1)
        printf("%d\n", n);
	fflush(stdout);
    return 0;
}

The process stops on readline. It seems a deadlock. Could you tell me why?

This 'blocking readline problem' is a reccurrent question about the subprocess module. If your OS is linux, I once found a simple solution which works very well http://www.daniweb.com/forums/post777876.html#post777876 . For a multiplatform solution, you should try and use the Popen subclass defined in this activestate recipe http://code.activestate.com/recipes/440554/. See the test code at the end of the recipe, and also check the comments.

And suggesting that jogged my memory about pexpect, which you can use for interacting with external programs http://www.noah.org/wiki/Pexpect#Description_of_Pexpect So now you have 2 good options and hopefully one of them will solve the problem.

Q: Why not just use a pipe (popen())?

A: A pipe works fine for getting the output to non-interactive programs. If you just want to get the output from ls, uname, or ping then this works. Pipes do not work very well for interactive programs and pipes will almost certainly fail for most applications that ask for passwords such as telnet, ftp, or ssh.

Thanks you guys! Your posts were really helpfull.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.