问题
Is there a way to pass data between continuously running C program and continuously running Python program? It is crucial that C program starts first.
So far I have (for C side):
void run_cmd(char *cmd[])
{
int parentID = getpid();
char str[1*sizeof(double)];
sprintf(str, "%d", parentID);
char* name_with_extension;
name_with_extension = malloc(2+strlen(cmd[1])+1*sizeof(int)+1);
strcat(name_with_extension, cmd[1]);
strcat(name_with_extension, " ");
strcat(name_with_extension, str);
pid_t pid;
char *argv[] = {"sh", "-c", name_with_extension, NULL};
int status;
//printf("Run command: %s\n", cmd);
status = posix_spawn(&pid, "/bin/sh", NULL, NULL, argv, environ);
if (status == 0) {
//printf("Child pid: %i\n", pid);
//printf("My process ID : %d\n", getpid());
//if (waitpid(pid, &status, 0) != -1) {
// printf("Child exited with status %i\n", status);
//} else {
// perror("waitpid");
//}
//part below is not tested and will probably not work
int myout[2];
pipe(myout);
int status;
int ch;
do {
if (read(myout[0], &ch, 1)>0){
write(1, &ch, 1);
}
waitpid(pid, &status, WNOHANG);
} while (!WIFEXITED(status) && !WIFSIGNALED(status));
}
}
For Python, I can only get the arguments list for now using:
print 'Arguments ', str(sys.argv)
As I understand from documentation, subprocess.Popen is not a way to go, since it creates a new process, which I do not want.
Embedding C in Python (or inverse) is not an option as code is too big.
I thought using process IDs and possibly sockets communicating data between, but not sure and need some advice.
The aim is to accomplish this in Windows, but unified single implementation would be better.
回答1:
You have a couple of options
- Pass data through stdin and output to stdout.
You'll have to devise a line-based format, read a line from stdin and print what you want to communicate to parent process.
See the example below
- Use an IPC mechanism for process communication
In this I'd propose using zmq. It's cross-platform and has quite a few features.
So, a bit of code in python showing the general idea with stdin/stdout communication
P1 (the child)
import sys
import time
i=0
while True:
line = sys.stdin.readline().strip()
if not line:
time.sleep(0.5)
if line=="ping":
sys.stdout.write("pong\n")
sys.stdout.flush()
i+= 1
if i > 10:
sys.stdout.write("exit\n")
sys.stdout.flush()
P2 (the master)
import subprocess
p = subprocess.Popen(['python', './p1.py'],stdout=subprocess.PIPE, stdin=subprocess.PIPE)
while True:
p.stdin.write("ping\n")
p.stdin.flush()
ret = p.stdout.readline().strip()
print ret
if ret=='exit':
exit(0)
P2 starts P1, they do 10 ping-pongs and p1 notifies p2 that it must kill itself. The processes can be long running.
来源:https://stackoverflow.com/questions/30241937/communicate-data-between-c-and-python-apps-running-continuously