C应用程序和Python之间的IPC [英] IPC between C application and Python
问题描述
因此,我是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:
- 将python程序作为主c程序的子进程调用
- 将包含要处理数据的c结构传递给python进程
- 将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
.
- Pipes (named pipes or anonymous)
- Unix domain sockets
TCP
orUDP
via the sockets API- Shared memory
- Message queues
pipe
方法是最简单的.请注意,当您在C程序和Python之间来回传递数据时,您将需要担心数据的传输语法.如果选择使用C结构(可以是不可移植的),则需要在Python方面解压缩数据.另外,您可以使用某些文本格式-sprintf
/sscanf
或JSON
等的组合.
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屋!