0

Ignoring the actual utility, I have the following classes:

from multiprocessing import Manager
import threading


class MgrStaticTarget(object):

    def __init__(self):
        self._mgr = Manager()
        self._thd = threading.Thread(target=self.async_targ)

    @staticmethod
    def async_targ():
        pass

class MgrInstanceTarget(object):

    def __init__(self):
        self._mgr = Manager()
        self._thd = threading.Thread(target=self.async_targ)

    def async_targ(self):
        pass

and I'm observing behavior I don't understand. Upon repeated instantiations of MgrInstanceTarget using a Python shell in the following manner:

i = MgrInstanceTarget()
i = MgrInstanceTarget()
i = MgrInstanceTarget()
i = MgrInstanceTarget()

the multiprocessing.Manager server processes (or at least that's what I believe they are from) are accumulating.

However, if I instantiate MgrStaticTarget in the same manner the server processes do not accumulate. The only processes that exist are the one for the most recent Manager server process and the python shell. What's going on here?

dano
  • 91,354
  • 19
  • 222
  • 219
harumph
  • 195
  • 1
  • 11
  • I think if you use a static target you do not create several instances as all point to the same target, in the other case the class is created several times... Not sure about that... – Anselm Scholz Jan 14 '15 at 03:31
  • 1
    Any particular reason you're creating a `Manager` for every instance of the class? I would just create one, global `Manager`, and then have each instance use that same `Manager` to create shared instances. – dano Jan 14 '15 at 16:20
  • 1
    You're seeing `Manager` processes accumlate with the `MgrInstanceTarget` because the garbage collector isn't running to clean up the old instances. If you run `import gc; gc.collect()`, they'll go away. That's actually the behavior I'd expect. That said, I'm not sure why you're *not* seeing that behavior with the `MgrStaticTarget`; I don't see processes accumulating with that object even if I disable the garbage collector altogether. – dano Jan 14 '15 at 16:31
  • @dano Thanks for the suggestion. To answer your first question, no, not a good reason :). I moved the Manager up to the class namespace and, as expected, no more process accumulation. – harumph Jan 14 '15 at 16:38

0 Answers0