13

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.

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?

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.

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?

Thanks!

1
  • The main problem you will encounter with a shared memory approach is one program seeing the value the other program wrote. Memory access semantics being what they are, it might be quite some time before the C++ code sees a value poked into memory somewhere by the Python code. Commented Apr 22, 2011 at 16:26

5 Answers 5

12

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

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

Sign up to request clarification or add additional context in comments.

Comments

5

Why not open a unix socket? Or use DBus

3 Comments

I've only used basic shared memory in some simple C programs, but why a socket/dbus over shared memory? Also, a socket seems like it could be overkill. Wouldn't shared memory be easier?
@Shickadance - typically it leads to simpler semantics. Message-based communication tend to scale very well. Also, the resulting code tends to be more portable (as shared memory code might differ from os to os.)
You can simply listen to an open socket for any noise on the line and enjoy the IPC
3

If Boost is an option, you could use Boost.Python and 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.

2 Comments

Does Boost.Ipc have python bindings? How would I use Boost.Ipc in python?
@EdisonGustavoMuenz - I doubt that boost.ipc has python bindings. You would need to use C++ as an intermediary.
1

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.

Comments

1

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.

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.)

1 Comment

It's possible, at the cost of kludging with ctypes. stackoverflow.com/a/1822807/418374 Not sure about its safety though.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.