I have a fairly complex Python object that I need to share between multiple processes. I launch these processes using multiprocessing.Process
. When I share an object with multiprocessing.Queue
and multiprocessing.Pipe
in it, they are shared just fine. But when I try to share an object with other non-multiprocessing-module objects, it seems like Python forks these objects. Is that true?
I tried using multiprocessing.Value. But I'm not sure what the type should be? My object class is called MyClass. But when I try multiprocess.Value(MyClass, instance)
, it fails with:
TypeError: this type has no size
Any idea what's going on?
You can do this using Python's multiprocessing
"Manager" classes and a proxy class that you define. See Proxy Objects in the Python docs.
What you want to do is define a proxy class for your custom object, and then share the object using a "Remote Manager" -- look at the examples in the same linked doc page in the "Using a remote manager" section where the docs show how to share a remote queue. You're going to be doing the same thing, but your call to your_manager_instance.register()
will include your custom proxy class in its argument list.
In this manner, you're setting up a server to share the custom object with a custom proxy. Your clients need access to the server (again, see the excellent documentation examples of how to setup client/server access to a remote queue, but instead of sharing a Queue
, you are sharing access to your specific class).