Python和C ++应用程序的简单但快速的IPC方法? [英] Simple but fast IPC method for a Python and C++ application?

查看:1903
本文介绍了Python和C ++应用程序的简单但快速的IPC方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用Python和C ++代码的GNU Radio应用程序。我想能够用信号通知事件的C ++代码。如果他们在同一个范围,我通常使用一个简单的布尔,但代码是分开的点,需要某种形式的共享内存。有关的代码是性能关键的,所以需要一个有效的方法。

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.

我最初想到一个共享内存段,可以访问的Python和C ++。因此,我可以在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 ++代码只需检查这个标志,如果设置进入一个忙循环。

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 ++之间实现共享内存段是一个合理的方法?信号量怎么样?在Linux上,这更容易实现?

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的信号量和共享内存的访问,并支持Python 2.4-3.1(而不是3.0): http:/ /semanchuk.com/philip/posix_ipc

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

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

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

这篇关于Python和C ++应用程序的简单但快速的IPC方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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