Reputation: 4460
I have a genetic expression tree program to control a bot. I have a GP.py and a MyBot.py. I need to be able to have the MyBot.py access an object created in GP.py
The GP.py is starting the MyBot.py via the os.system() command
I have hundreds of tree objects in the GP.py file and the MyBot.py needs to evaluate them.
I can't combine the two into one .py file because a fresh instance of MyBot.py is executed thousands of times, but GP.py needs to evaluate the fitness of MyBot.py with each tree.
I know how to import the methods and Class definitions using import GP.py, but I need the specific instance of the Tree class object Any ideas how to send this tree from the first instance to the second?
Upvotes: 1
Views: 918
Reputation: 308
We are developing an ORB interaction framework Versile Python (AGPL) which can be used for such inter-process object-level communication. It is in development but you may want to have a look. This recipe shows how you could use pipes between GP.py and MyBot.py to access remote objects. You can use the link with native python objects to get direct access to python objects, or write custom external objects.
Upvotes: 0
Reputation: 59604
Usually in such cases RPC is used. In python i usually use XML-RPC:
http://docs.python.org/library/simplexmlrpcserver.html
http://twistedmatrix.com/documents/current/web/howto/xmlrpc.html
This is a more flexible solution than passing data via files, and works even if the processes are on different machines.
Upvotes: 0
Reputation: 884
You could serialize the object with the pickle module (or maybe json?)
If you really want to stick with os.system, then you could have MyBot.py write the pickled object to a file, which GP.py could read once MyBot.py returns.
If you use the subprocess module instead, then you could have MyBot.py write the pickled object to stdout, and GP.py could read it over the pipe.
If you use the multiprocessing module, then you could spawn the MyBot process and pass data back and forth over a Queue instance.
Upvotes: 2