shared lib from python code?

Duncan Booth duncan.booth at invalid.invalid
Mon Feb 23 08:09:15 EST 2009


Gabriel Rossetti <gabriel.rossetti at arimaz.com> wrote:

> Ok, maybe I mis-stated my problem (or mis-understood your answers).. I 
> don' t want to share code as in have multiple processes access a 
> variable and have the same value, like it is done in threads, what I 
> want is to not have n copies of the code (classes, functions, etc) 
> loaded by n python interpreters. When you load Apache for instance, it 
> runs n processes but loads only one copy of parts of it's code (so/dll), 
> that's what I want to do. In C/C++ I would write a shared-lib (so/dll), 
> so once the system has loaded it, it doesn' t re-load it when another 
> process needs it.
> 

It doesn't matter how you restate it, the answer is still the one Gabriel 
Genellina gave you: Python code objects are not shared between processes.

Code objects are immutable, but they are still objects like everything else 
in Python. That means they are reference counted and discarded when no 
longer referenced.

To share code objects between different processes you would have to change 
the reference counting mechanism so it was either safe across multiple 
processes or didn't reference count shared code objects (but still ref 
counted non-shared objects).

The actual code is just stored in a string, so perhaps you could share 
those strings between processes: just executing the code doesn't change the 
string's refcount (whereas it does change the function and the code 
objects' counts). You could probably make something work simply by editing 
the code constructor to move the code into shared memory and using some 
other mechanism to control the shared memory storage.

C extension libraries on the other hand may be shared.

-- 
Duncan Booth http://kupuguy.blogspot.com



More information about the Python-list mailing list