Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I store a Python object in memory for use by different processes?

Here's the situation: I have a massive object that needs to be loaded into memory. So big that if it is loaded in twice it will go beyond the available memory on my machine (and no, I can't upgrade the memory). I also can't divide it up into any smaller pieces. For simplicity's sake, let's just say the object is 600 MB and I only have 1 GB of RAM. I need to use this object from a web app, which is running in multiple processes, and I don't control how they're spawned (a third party load balancer does that), so I can't rely on just creating the object in some master thread/process and then spawning off children. This also eliminates the possibility of using something like POSH because that relies on it's own custom fork call. I also can't use something like a SQLite memory database, mmap or the posix_ipc, sysv_ipc, and shm modules because those act as a file in memory, and this data has to be an object for me to use it. Using one of those I would have to read it as a file and then turn it into an object in each individual process and BAM, segmentation fault from going over the machine's memory limit because I just tried to load in a second copy.

There must be someway to store a Python object in memory (and not as a file/string/serialized/pickled) and have it be accessible from any process. I just don't know what it is. I've looked all over StackOverflow and Google and can't find the answer to this, so I'm hoping somebody can help me out.

like image 478
Brendan Avatar asked Dec 12 '11 02:12

Brendan


People also ask

How Python objects are stored 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.

What Stores single Python object?

A list is a type of Python object that allows us to store a sequence of other objects. One of its major utilities is that it provides us with means for updating the contents of a list later on.

How do I allocate more memory to Python?

Python doesn't limit memory usage on your program. It will allocate as much memory as your program needs until your computer is out of memory. The most you can do is reduce the limit to a fixed upper cap. That can be done with the resource module, but it isn't what you're looking for.

How do you find the memory location of an object in Python?

Method 1: Using id() We can get an address using the id() function. id() function gives the address of the particular object. where the object is the data variables.


1 Answers

http://docs.python.org/library/multiprocessing.html#sharing-state-between-processes

Look for shared memory, or Server process. After re-reading your post Server process sounds closer to what you want.

http://en.wikipedia.org/wiki/Shared_memory

like image 120
Derek Litz Avatar answered Oct 13 '22 05:10

Derek Litz