C应用程序和Python之间的IPC [英] IPC between C application and Python

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

问题描述

因此,我是IPC的新手,我有一个收集数据的c程序和一个分析数据的python程序.我希望能够:

So I am relatively new to IPC and I have a c program that collects data and a python program that analyses the data. I want to be able to:

  1. 将python程序作为主c程序的子进程调用
  2. 将包含要处理数据的c结构传递给python进程
  3. 将python进程中的int值返回给c程序

我一直在简短地研究Pipes和FIFO,但是到目前为止,找不到任何信息来解决此类问题,因为据我了解,例如fork()只会复制调用过程,所以我不能想要,因为我正在尝试调用其他进程.

I have been briefly looking at Pipes and FIFO, but so far cannot find any information to address this kind of problem, since as I understand it, a fork() for example will simply duplicate the calling process, so not what I want as I am trying to call a different process.

推荐答案

关于 fork() ,并且需要执行其他过程. fork()确实创建了当前进程的副本.但这通常与 exec() (其中之一各种形式)以获取流程副本以执行其他程序.

About fork() and the need to execute a different process. It is true that fork() creates a copy of the current process. But this is usually coupled with exec() (one of the various forms) to get the process copy to execute a different program.

对于 IPC ,您有几种选择.有人提到一个队列-但是ZeroMQ之类的东西实在是太过分了.您可以使用几种机制之一进行IPC.

  1. 管道(命名管道或匿名管道)
  2. Unix域套接字
  3. 通过套接字API
  4. TCPUDP
  5. 共享内存
  6. 消息队列
  1. Pipes (named pipes or anonymous)
  2. Unix domain sockets
  3. TCP or UDP via the sockets API
  4. Shared memory
  5. Message queues

pipe方法是最简单的.请注意,当您在C程序和Python之间来回传递数据时,您将需要担心数据的传输语法.如果选择使用C结构(可以是不可移植的),则需要在Python方面解压缩数据.另外,您可以使用某些文本格式-sprintf/sscanfJSON等的组合.

The pipe approach is the easiest. Note that when you pass data back and forth between the C program and Python, you will need to worry about the transfer syntax of the data. If you choose to use C structs (which can be non portable), you will need to unpack the data on the Python side. Else you can use some textual format - combination of sprintf/sscanf, or JSON etc.

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

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