TIL: Python shared_memory.ShareableList

While watching Tech Tangents over the weekend, I went digging into the Python documentation to see what features Python has for doing shared memory between two processes. TT was attempting to do it with multiprocessing.Value() objects, but was using pointer-ctypes, which are only valid in the memory space they’re created in (ie, they’re not accessible between processes).

This led to discovering shared_memory.ShareableList. It has some significant limitations compared to a standard list object in Python, but if all you need is a way to pass a string between two processes, it’ll work.

The restriction is basically that the number of bytes in shared memory for the ShareableList object is allocated at creation time, and cannot be expanded. So no using it to pass a list that you can append to, unless you allocated out several megabytes of memory for it at startup.


Tags: