Multiprocessing Share Unserializable Objects Between Processes

后端 未结 3 2102
抹茶落季
抹茶落季 2020-12-03 07:38

There are three questions as possible duplicates (but too specific):

  • How to properly set up multiprocessing proxy objects for objects that already exist
3条回答
  •  日久生厌
    2020-12-03 08:37

    Most of the time it's not really desirable to pass the reference of an existing object to another process. Instead you create your class you want to share between processes:

    class MySharedClass:
        # stuff...
    

    Then you make a proxy manager like this:

    import multiprocessing.managers as m
    class MyManager(m.BaseManager):
        pass # Pass is really enough. Nothing needs to be done here.
    

    Then you register your class on that Manager, like this:

    MyManager.register("MySharedClass", MySharedClass)
    

    Then once the manager is instanciated and started, with manager.start() you can create shared instances of your class with manager.MySharedClass. This should work for all needs. The returned proxy works exactly like the original objects, except for some exceptions described in the documentation.

提交回复
热议问题