share dictionary between processes

r0g aioe.org at technicalbloke.com
Fri Dec 18 08:27:30 EST 2009


blumenkraft wrote:
> Hi,
> 
> I want to share dictionary between two distinct processes.
> 
> 
> Something like this:
> 
> first.py
> import magic_share_module
> 
> def create_dictionary():
>     return {"a": 1}
> 
> magic_share_module.share("shared_dictionary",
> creator.create_dictionary)
> while True:
>      pass
> 
> 
> second.py
> import magic_share_module
> d = magic_share_module.get_shared("shared_dictionary")
> print d["a"]
> 
> And then run in command line:
> python first.py &
> sleep 1
> python second.py
> 
> I have looked at POSH, but it requires master process that will fork
> childs. I want read-only sharing between completely unrelated
> processes.
> Is it possible?



Depends if you need to have access to the object itself or merely look
up values in it. If it's the latter you could start a thread in first.py
that offers dictionary lookups via a specific UDP socket. Any number of
subsequent progs and processes could then pitch up and lookup whatever
values they like whenever they like. ~10 lines of code in first.py,
maybe 5 in second.py

Roger.



More information about the Python-list mailing list