boost Build Tools CMake Dotnet Core Libraries Python
Jared Rhodes  

Communicating between Python and .NET Core with Boost Interprocess

To see if I could, I put together a cross communication library for .Net Core and Python applications using Boost.Interprocess, Boost.Python, and Boost.Signals2. The goal was simple, expose the same interface for cross communication to C# and Python. The approach taken was to use the condition example and edit it to expose to the different languages.

Shared Definitions

First I need to create the objects to make the interface. There are four files making up these objects:

  • shm_remove.hpp – just a lifecycle object to clear the shared buffer when it is destructed
  • TraceQueue.hpp – The shared memory object
  • SharedMemoryConsumer.hpp – The subscriber to the shared memory data
  • SharedMemoryProducer.hpp – The publisher for the shared memory data

https://gist.github.com/QiMata/e7192eb93a0910787e0244241351e5e9

These objects comprise the core interface of the shared memory provider. Now, the memory providers need to be exposed to multiple languages. There are different ways to do this and I decided to do it by hand. I should point out SWIG is my usual approach to this task, however, in this instance it seemed easy enough to do it by hand.

Boost Python

To expose the python code, I needed to create a few classes to expose the interface definitions to Boost.Python. The two classes are:

  • PythonSharedMemoryConsumer.hpp – The python interface for the SharedMemoryConsumer
  • PythonModule.cpp¬† – The file that exposes the module to python

https://gist.github.com/QiMata/33d364ac9873eca30413ccb5a7606fa8

These two classes combine to expose the files to python and can be used in a python script by just importing the shared library.

.NET Core

With the python portion complete, I needed to expose the shared memory objects to CSharp. This is easy enough to do by hand if you expose the classes to be used by PInvoke. To accomplish this, I only needed three files:

  • NetCoreSharedMemoryProducer.hpp – The .NET Core version of the publisher
  • NetCoreSharedMemoryConsumer.hpp – The .NET Core version of the consumer
  • NetCoreModule.cpp – The source file exposing the interfaces for PInvoke

 

https://gist.github.com/QiMata/a702627eb38250e28142773bda03719b

https://gist.github.com/QiMata/a702627eb38250e28142773bda03719b

Now we need to call that code from C# using PInvoke Interop

https://gist.github.com/QiMata/a8b08811c7eb37d04756909ce5531570

 

4 thoughts on “Communicating between Python and .NET Core with Boost Interprocess

  1. […] Communicating between Python and .NET Core with Boost Interprocess – Jared Rhodes […]

  2. thelazydogsback

    ITSM that using memory-mapped files or just direct localhost/loopback sockets would both be easier to implement and performant??

    1. Jared Rhodes

      I don’t have performance numbers between the different communication patterns.

  3. thelazydogsback

    There’s also good ol’ stdio…
    https://code.msdn.microsoft.com/windowsdesktop/C-and-Python-interprocess-171378ee

Leave A Comment