memory sharing with non-python applications

i had a thought the other day: you can get the memory address of a python variable using the id() built-in function, so surely you could save this address in a file, open it in a C or C++ program and put it in a pointer variable. Then you would be able to read/write to the memory address in both python and C/C++

Is there any reason why that wouldn’t work? Have I just completely lost the plot?? :slight_smile:

Keith. 8)

Criminy, that sounds a bit dangerous!
For three reasons I can think of:
a) AFAIK, the id() function isn’t GUARANTEED to give the memory address. I believe that’s just what the current implementation does.
b) How do you know what format the memory is in, or even how much memory you need to access for a particular data type? (Hmm, well, I guess the source code would help here)
c) How do you know that the Python engine won’t unexpectedly change the location a variable is stored in (I dunno if this would happen, but it’s possible)

Also, it could be tricky getting shared access to the memory.

Just my 2 pesete :slight_smile:

artificer

oh well, it was just a thought. :-?

in linux there is something regarding shared memory, which may work

I don’t know, I’ve not used it before

The way suggested (save a pointer to a file, load in a different application) won’t work. The reason is that modern operating systems separate the memory of each program - this means a buggy program can’t take down other programs.

You can however, use the OS provided API’s to talk to different applications. On Linux, you can use IPC, sockets, pipes, and shared memory as z3ro d said. On Windows, you can send messages to other applications message queues.

Or, since you are using Python anyway, you can write a Python interface to your C/C++ code and access it directly.

How would you do that? embed python code using “python.h” in a C/C++ DLL file? Im not much of a C/C++ programmer so i’d find that really hard.

Is there any simpler way? Specifically im asking about doing it in the realtime python implementation, which only uses python 2.1. But I’d like to know how to do it in 2.2 as well.

Cheers,
Keith. 8)

You should look at the python/C api documentation. Maybe this will help.
parsing python variables into C
python/c api
embedding python in a C program
Object identities
blender’s python api