我该如何处理C和Python的IPC? [英] How can I handle IPC between C and Python?

查看:270
本文介绍了我该如何处理C和Python的IPC?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个过程,一个是C,一个Python中的一个应用程序。在C工艺是所有繁重完成,而Python的过程中处理用户界面。

I have a an application with two processes, one in C and one in Python. The C process is where all the heavy lifting is done, while the Python process handles the user interface.

C程序写入到一个大十岁上下的缓冲每秒4次,和Python的进程读取这些数据。这点对Python的过程中,通信已被AMQP完成。我宁愿设置一些为两个进程之间的内存共享来减少开销,并提高性能。

The C program writes to a large-ish buffer 4 times per second, and the Python process reads this data. To this point the communication to the Python process has been done by AMQP. I would much rather setup some for of memory sharing between the two processes to reduce overhead and increase performance.

什么是我选择这里?理想情况下我只想有Python的进程读取物理内存直(从内存preferable,而不是从盘),然后照顾的比赛条件,信号量或类似的东西。然而,这是我有经验少,所以我倒是AP preciate任何帮助,我可以得到的。

What are my options here? Ideally I would simply have the Python process read the physical memory straight (preferable from memory and not from disk), and then taking care of race conditions with Semaphores or something similar. This is however something I have little experience with, so I'd appreciate any help I can get.

我顺便说一下使用Linux。

I am using Linux btw.

推荐答案

建议#1:
最简单的方法应该是使用TCP。你提到你的数据大小大。除非你的数据量过于庞大,你应该使用TCP罚款。确保您在C和Python的不同线程发送/通过TCP接收数据。

Suggestion #1: The simplest way should be using TCP. You mentioned your data size is large. Unless your data size is too huge, you should be fine using TCP. Ensure you make separate threads in C and Python for transmitting/receiving data over TCP.

建议2:
Python支持包装过C.一种流行的包装为的ctypes - HTTP: //docs.python.org/2/library/ctypes.html

假设你是通过共享内存的两个C程序之间熟悉工控机,你可以写一个C-包装为你的Python程序从共享内存中读取数据。

Assuming you are familiar with IPC between two C programs through shared-memory, you can write a C-wrapper for your python program which reads data from the shared memory.

另外,还要检查以下diccussion其中Python和C ++之间谈论IPC:
<一href=\"http://stackoverflow.com/questions/6915191/simple-ipc-between-c-and-python-cross-platform\">Simple C的IPC ++和Python(跨平台)

Also check the following diccussion which talks about IPC between python and C++: Simple IPC between C++ and Python (cross platform)

这篇关于我该如何处理C和Python的IPC?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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