Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Placing Python objects in shared memory

Is there a Python module that would enable me to place instances of non-trivial user classes into shared memory?

By that I mean allocating directly in shared memory as opposed to pickling into and out of it.

multiprocessing.Value and multiprocessing.Array wouldn't work for my use case as they only seem to support primitive types and arrays thereof.

The only thing I've found so far is POSH, but it hasn't changed in eight years. This suggests that it's either super-stable or is out of date. Before I invest time in trying to get it work, I'd like to know if there are alternatives I haven't discovered yet.

I only need this to work on Linux.

like image 662
NPE Avatar asked Jun 07 '11 19:06

NPE


People also ask

What is shared memory in Python?

Shared memory can be a very efficient way of handling data in a program that uses concurrency. Python's mmap uses shared memory to efficiently share large amounts of data between multiple Python processes, threads, and tasks that are happening concurrently.

How does Python store objects in memory?

Python uses a garbage collection algorithm (called Garbage Collector) that keeps the Heap memory clean and removes objects that are not needed anymore. You don't need to mess with the Heap, but it is better to understand how Python manages the Heap since most of your data is stored in this section of the memory.

How are Python objects represented in memory?

In CPython, which is what most people use when they use python , all Python objects are represented by a C struct, PyObject .

How do I create a shared memory object?

Open (or create) a shared-memory region. Close a shared-memory region. Map a shared-memory region into a process's address space. Unmap a shared-memory region from a process's address space.


1 Answers

That's kind of a tough one. The best solution I can think of is pickling your objects and using a c_char_p with multiprocessing.sharedctypes. You'd still have to make sure no null bytes got into the c_char_p, either by escaping them or converting to hex.

On second thought, maybe you should go with POSH.

like image 124
Nick ODell Avatar answered Oct 07 '22 09:10

Nick ODell