Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: how to share an object instance across multiple invocations of a script

I'm use a library which provides a python interface to an external program. This allows me to create:

foo = Foo()

The code above starts a new instance of the Foo program that I can control from within python.

I have a python scripts which needs to be invoked multiple times and depending on external parameters, tell a single instance of the external Foo program to do different things. Obvious I can't do

foo = Foo() everytime,

since that creates a new instance of Foo every time my script runs.

What I want to do is to create foo= Foo() once, and have multiple invocations share the same instance. Currently I'm thinkibng of just creating it once, serialize it, and have my scripts deserialize it. Does this approach work? Is there a better alternative?

Thanks!!

like image 816
wk1989 Avatar asked Jun 26 '11 01:06

wk1989


1 Answers

This can be done if you follow an approach similar to that given in this answer. Or you can use Pyro, which is compared to multiprocessing in this answer.

like image 86
Vinay Sajip Avatar answered Oct 15 '22 07:10

Vinay Sajip