Linux 用于 Python 和 C++ 应用程序的简单但快速的 IPC 方法?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5756813/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 03:47:09  来源:igfitidea点击:

Simple but fast IPC method for a Python and C++ application?

c++pythonlinuxipc

提问by Mr. Shickadance

I have a GNU Radio application which utilizes both Python and C++ code. I want to be able to signal the C++ code of an event. If they were in the same scope I would normally use a simple boolean, but the code is separate to the point where some form of shared memory is required. The code in question is performance-critical so an efficient method is required.

我有一个 GNU Radio 应用程序,它同时使用 Python 和 C++ 代码。我希望能够发出事件的 C++ 代码信号。如果它们在同一个范围内,我通常会使用一个简单的布尔值,但代码是分开的,需要某种形式的共享内存。有问题的代码对性能至关重要,因此需要一种有效的方法。

I was initially thinking about a shared memory segment that is accessible by both Python and C++. Therefore I could set a flag in the python code and check it from C++. Since I just need a simple flag to pause the C++ code, would a semaphore suffice?

我最初考虑的是一个可由 Python 和 C++ 访问的共享内存段。因此我可以在 python 代码中设置一个标志并从 C++ 中检查它。因为我只需要一个简单的标志来暂停 C++ 代码,一个信号量就足够了吗?

To be clear, I need to set a flag from Python and the C++ code will simply check this flag, and if it is set enter a busy loop.

明确地说,我需要从 Python 设置一个标志,C++ 代码将简单地检查这个标志,如果设置了它,则进入一个忙循环。

So would trying to implement a shared memory segment between Python/C++ be a reasonable approach? How about a semaphore? On Linux, which is easier to implement?

那么尝试在 Python/C++ 之间实现共享内存段是一种合理的方法吗?信号量怎么样?在 Linux 上,哪个更容易实现?

Thanks!

谢谢!

采纳答案by Joseph Lisee

Assuming this is two separate applications on one machine and you need decent real time performance you don't want to go with sockets. I would use a flag in shared memory, and probably use a semaphore to make sure both programs can't be accessing the flag at once. This library provides access to the semaphores and shared memory with Python and supports Python versions 2.4-3.1 (not 3.0): http://semanchuk.com/philip/posix_ipc

假设这是一台机器上的两个独立应用程序,并且您需要不错的实时性能,而不想使用套接字。我会在共享内存中使用一个标志,并且可能使用一个信号量来确保两个程序不能同时访问该标志。该库通过 Python 提供对信号量和共享内存的访问,并支持 Python 版本 2.4-3.1(不是 3.0):http://semanchuk.com/philip/posix_ipc

EDIT: Changed recommendation to using a semaphore protecting the flag in shared memory

编辑:更改了使用信号量保护共享内存中的标志的建议

回答by Dr McKay

DBus looks promising. It supports signals, so you should be able to stop an application on demand. However, I'm not sure if it's performance will be enough for you.

DBus 看起来很有希望。它支持信号,因此您应该能够按需停止应用程序。但是,我不确定它的性能是否足以满足您的需求。

回答by Jakob Bowyer

Why not open a unix socket? Or use DBus

为什么不打开一个unix socket?或者使用DBus

回答by luis.espinal

You can try using custom signals. I don't know about Python code being able to send custom signals, but your C/C++ can certainly define custom signals with SIGIO.

您可以尝试使用自定义信号。我不知道 Python 代码能够发送自定义信号,但是您的 C/C++ 当然可以使用 SIGIO 定义自定义信号。

If you have stringent response-time requirements, you might need to look beyond your application code and into some time of OS with support for real-time signals (rt-linux, muOs, etc.)

如果您有严格的响应时间要求,您可能需要超越您的应用程序代码和支持实时信号(rt-linux、muOs 等)的操作系统的某些时间。

回答by Ferruccio

If Boost is an option, you could use Boost.Pythonand Boost.Interprocess. Boost.Python gives you a way for Python & C++ objects to interact and Boost.Interprocess gives you plenty of options for shared memory or synchronization primitives across process boundaries.

如果 Boost 是一个选项,您可以使用Boost.PythonBoost.Interprocess。Boost.Python 为 Python 和 C++ 对象提供了一种交互方式,Boost.Interprocess 为您提供了大量跨进程边界的共享内存或同步原语选项。