在连续运行的C和Python应用程序之间通信数据 [英] Communicate data between C and Python apps running continuously

查看:141
本文介绍了在连续运行的C和Python应用程序之间通信数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法在连续运行的C程序和连续运行的Python程序之间传递数据? C程序首先启动至关重要.

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.

到目前为止(对于C端):

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));

    }
}

对于Python,我现在只能使用以下命令获取参数列表:

For Python, I can only get the arguments list for now using:

print 'Arguments ', str(sys.argv)

据我从文档中了解到, subprocess.Popen 并非可行之路,因为它创建了我不希望的新流程.

As I understand from documentation, subprocess.Popen is not a way to go, since it creates a new process, which I do not want.

嵌入 在Python(或反向)中不是C的选择.

Embedding C in Python (or inverse) is not an option as code is too big.

我认为使用进程 ID 和可能的 套接字 之间的数据通信,但不确定并需要一些建议

I thought using process IDs and possibly sockets communicating data between, but not sure and need some advice.

目标是在 Windows 中完成此操作,但是统一的单一实现会更好.

The aim is to accomplish this in Windows, but unified single implementation would be better.

推荐答案

您有几个选择

  1. 通过stdin传递数据并输出到stdout.

您将必须设计一种基于行的格式,从stdin中读取一行,并打印要与父进程进行通信的内容.

You'll have to devise a line-based format, read a line from stdin and print what you want to communicate to parent process.

请参见下面的示例

  1. 使用IPC机制进行过程通信

在这种情况下,我建议使用zmq.它是跨平台的,具有很多功能.

In this I'd propose using zmq. It's cross-platform and has quite a few features.

因此,python中的一些代码展示了stdin/stdout通信的总体思路

So, a bit of code in python showing the general idea with stdin/stdout communication

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(主机)

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启动P1,他们进行10次乒乓球,然后p1通知p2它必须杀死自己.这些过程可能会长时间运行.

P2 starts P1, they do 10 ping-pongs and p1 notifies p2 that it must kill itself. The processes can be long running.

这篇关于在连续运行的C和Python应用程序之间通信数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆