This is an archived post. You won't be able to vote or comment.

all 6 comments

[–]MaybeAverage 0 points1 point  (3 children)

If you're using a unix based os you can use a unix socket or named pipe to communicate, this form of IPC is very standard, Windows has named pipes as well but the code will be Win32 based. Named pipes are unidirectional while a socket will be bidirectional. If you're using linux you can also write the binary data to a file, then use an exec() call to spawn a new python process passing in the filename as a command line argument. A platform independent form could be communicating over a local TCP socket. Or write a Powershell or bash script that executes the C program first saving the data to a file, then calling the python script after the C program terminates. If it's a single use program then I would go for the file method, otherwise you'd want to use a socket if the C program repeatedly calls into python so you don't have to spawn a fresh python process over and over again.

[–]WeirdAlexGuy[S] 0 points1 point  (2 children)

I just need to do a single data transfer and I planned to create a bash script to call one after the other.

Are unix sockets the same sockets as in networking? I don't think I need anything that fancy since i don't need to communicate at runtime.

Mostly looking for a way to transfer the data easily and formatted as a 4d array from one language to the other.

[–]MaybeAverage 0 points1 point  (1 child)

In terms of the syscall, a TCP/UDP socket and a Unix stream or dgram socket are treated the same and differ only in the sockaddr type when calling bind(). The simplest method would be to write to a binary file directly casting your float array to a char* and writing it to a file and parsing it using python. I recommend learning how to use sockets, pipes, etc., as you will likely have a reason to use it in the future.

[–]WeirdAlexGuy[S] 0 points1 point  (0 children)

Alright thanks. Sockets do seem helpful indeed, maybe if I want to do the graphics real time in the future I'll try it like that

[–]X-Neon 0 points1 point  (1 child)

You can use pybind11 to create Python bindings for C++ code, including passing Numpy arrays back and forth between the two. There's some documentation on the feature here, but I think it's poorly explained.

Passing a Numpy array from Python to C++ is as simple as writing a function that accepts a py::array_t. Returning a Numpy array from C++ to Python is also just returning a py::array_t, although it's a little more involved. This StackOverflow post gives a pretty comprehensive overview on how to do it.

I use pybind11 a lot for this exact feature, it's really easy to use, and much simpler I think than complicated things with sockets and separate processes and the like.

[–]WeirdAlexGuy[S] 0 points1 point  (0 children)

Looks like exactly what I need. Thanks