Home:ALL Converter>Access numpy array from separate C process using shared memory

Access numpy array from separate C process using shared memory

Ask Time:2016-01-20T00:26:08         Author:MRocklin

Json Formatter

I have a 1-D numpy array in memory

>>> x = np.arange(5)

I want to share this data with a separate and independent (not forked) C process on the same computer using shared memory.

I expect to do something like the following:

  1. Allocate a new block of shared memory from within Python
  2. Copy current data into that block
  3. Get dtype, length, and global address of the array and pass these to a C process (for now, we'll pass to the C process over a command line interface)
  4. Create an appropriately typed pointer within C to the memory address
  5. Do some trivial computation in C

What is the best way to acheive these steps? There appear to be several Python solutions to allocate array data in shared memory. All examples I can find involve sharing between two Python processes rather than between Python and another language.

Minimal examples are greatly welcomed.

Author:MRocklin,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/34881914/access-numpy-array-from-separate-c-process-using-shared-memory
yy