8

I have the following class that is shared between multiple consumers (using producer/consumer methodology). My question involves the methods called on this class. Do I need to implement locks or is the manager class thread safe?

import multiprocessing as mp
from multiprocessing.manager import BaseManager

class SampleClass(object):

    def __init__(self):
        self._count = 0

    # Does locking need to be implemented here?
    def increment(self):
        self._count += 1

BaseManager.register('SampleClass', SampleClass)
manager = BaseManager()
manager.start()

instance = manager.SampleClass()

jobs = []
for i in range(0, 5):
    p = mp.Process(target=some_func, args=(instance,))
    jobs.append(p)
    p.start()

for p in jobs:
    p.join()
Aaron Phalen
  • 421
  • 2
  • 7
  • 11

1 Answers1

0

I think so. As said as a comment on this other question:

  1. multiprocessing: How do I share a dict among multiple processes?

Is manager.dict() this process safe?

@LorenzoBelli, if you're asking whether access to the manager is synchronized, I believe the answer is yes. multiprocessing.Manager() returns an instance of SyncManager, the name of which suggests as much!

Community
  • 1
  • 1
Evandro Coan
  • 8,560
  • 11
  • 83
  • 144