在长时间运行的c进程和python之间进行双向IPC的最佳方法是什么?(what is the best way to do bidirectional IPC between a long-running c process and python?)

我有一个现有的C进程,可以输入一个文本并生成一个图像文件。 由于该过程与外部系统的接口,因此该过程具有较高的设置/拆卸成本。 一旦设置/拆除发生,从文本实际生成的图像几乎是瞬间完成的。

我的计划是守护进程,因此它将在无限循环中接收文本并生成图像文件,同时保持与外部系统的连接。

我还将在python中编写一个小型客户端程序,它将与守护进程交互以发送文本/接收图像。

目标操作系统是unix。

问题是,在这种情况下,在python / C之间进行双向IPC的最佳方法是什么? 我应该只打开一个unix域套接字并来回发送打包的结构,还是应该看看像Apache Thrift或protobuf这样的东西?

更新:

只是保持简单,并打开一个unix域套接字

I have an existing C process that can take one text input and produce a single image file. This C process has a high setup/teardown cost due to it's interface with an external system. Once the setup/teardown has occured the actual production of image from text is almost instantaneous.

My plan is to daemonize the C process, so it will receive text and produce image files in an infinite loop, while maintaining a connection to the external system.

I will also write a small client program in python, which will interface with the daemon to send text/receive the image.

The target OS is unix.

The question is, what is the best way to do bidirectional IPC between python/C in this case? Should I just open a unix domain socket and send packed structs back and forth, or should I look at something like Apache Thrift or protobuf?

UPDATE:

Just going to keep it simple, and open a unix domain socket

最满意答案

套接字是我想的方式。 在Unix上,我建议使用AF_UNIX套接字(参见unix(7)联机帮助页)。 这些很容易在C ++和python( sockets模块)中创建。 这可以避免端口冲突或本地系统上打开端口的权限问题。

Unix套接字表现相当不错,如果您决定与远程工作人员合作,可以轻松地交换AF_INET6套接字。

对于打包/解包数据,使用已编译的Struct对象的struct模块对我来说似乎是合理的。 多数民众赞成在过去我是如何做到的,而且表现相当不错(没有进行任何测量,因为这对我来说太好了调查)。

Sockets are the way to go here I think. On Unix, I would recommend the AF_UNIX sockets (see unix(7) manpage). Those are easily created in both C++ and python (sockets module). This avoids problems with port collisions or rights to open ports on the local system.

Unix sockets perform reasonably well and can easily be exchanged for AF_INET6 sockets if you decide to work with remote workers.

For packing/unpacking the data, the struct module using the compiled Struct objects seems reasonable for me. Thats how I have done it in the past and the performance was quite good (no measurements taken, as it was too good for me to investigate).

更多推荐