简体   繁体   English

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

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

I have a GNU Radio application which utilizes both Python and C++ code. 我有一个GNU Radio应用程序,它使用Python和C ++代码。 I want to be able to signal the C++ code of an event. 我希望能够发出事件的C ++代码信号。 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. 有问题的代码对性能至关重要,因此需要一种有效的方法。

I was initially thinking about a shared memory segment that is accessible by both Python and C++. 我最初考虑的是Python和C ++都可以访问的共享内存段。 Therefore I could set a flag in the python code and check it from C++. 因此,我可以在python代码中设置一个标志,并从C ++中检查它。 Since I just need a simple flag to pause the C++ code, would a semaphore suffice? 由于我只需要一个简单的标志来暂停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? 那么尝试在Python / C ++之间实现共享内存段是一种合理的方法吗? How about a semaphore? 信号量怎么样? On Linux, which is easier to implement? 在Linux上,哪个更容易实现?

Thanks! 谢谢!

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 编辑:更改建议使用保护共享内存中的标志的信号量

Why not open a unix socket? 为什么不打开unix socket? Or use DBus 或者使用DBus

If Boost is an option, you could use Boost.Python and Boost.Interprocess . 如果Boost是一个选项,您可以使用Boost.PythonBoost.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.Python为Python和C ++对象提供了一种交互方式,Boost.Interprocess为跨进程边界的共享内存或同步原语提供了大量选项。

DBus looks promising. DBus看起来很有前途。 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. 但是,我不确定它的性能是否足够。

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等)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM