6

My question is about the correct usage of multiprocessing.Managers.BaseManager. The main example in the documentation shows how to customize this class by creating a subclass of BaseManager. I can understand if someone does this in order to modify or extend the behavior of BaseManager.

But on the net and even in an answer on Stackoverflow people demonstrate the usage of BaseManager by creating a subclass that doesn't change the behaviour of BaseManager at all:

class MyManager(BaseManager): pass

My question is: What's the point in doing that in such an case? Why not use BaseManager and BaseManager.register() directly? Am I missing something?

EDIT: I found even more examples: here and here.

Community
  • 1
  • 1
ThoWe
  • 346
  • 3
  • 13

1 Answers1

6

BaseManager.register is a classmethod. This means that if you call register on a BaseManager instance, all other pieces of code in your project will see the registered type as well. This becomes an issue if one of those other pieces of code tries to do the same thing, i.e., register their own type directly with the BaseManager, using the same names as you do. Subclassing BaseManager is a way to avoid this namespace pollution.

If you are certain that this issue will never affect you - which, for example, is the case if you are writing a program, and not a library, and you are quite certain that you will not extend it later in some manner that could break things - then using BaseManager.register is absolutely fine. (Note though that this opinion is debatable. I think that this is fine because I prefer the pragmatic approach. Another view is that at the point when you finally want to extend your program, you likely have forgotten about this thing that will break, and therefore it is a good idea to make it right in the first place..)

Community
  • 1
  • 1
Phillip
  • 13,448
  • 29
  • 41