title: Sharing object (class instance) using multiprocessing Managers
body: <p>I need to share an object and its methods between several processes in python. I am trying to use <code>managers</code> (in module <code>multiprocessing</code>) but it crashes. Here is a silly example of producer-consumer where the shared object between the two processes is just a list of numbers with four methods.</p>
  <pre><code>from multiprocessing import Process, Condition, Lock  
  from multiprocessing.managers import BaseManager  
  import time, os  
    
  lock = Lock()  
  waitC = Condition(lock)  
  waitP = Condition(lock)  
    
  class numeri(object):  
      def __init__(self):  
          self.nl = []  
        
      def getLen(self):  
          return len(self.nl)  
        
      def stampa(self):  
          print self.nl  
            
      def appendi(self, x):  
          self.nl.append(x)  
        
      def svuota(self):  
          for i in range(len(self.nl)):  
              del self.nl[0]  
        
  class numManager(BaseManager):  
      pass  
    
  numManager.register('numeri', numeri, exposed = ['getLen', 'appendi', 'svuota', 'stampa'])  
    
  def consume(waitC, waitP, listaNumeri):  
      lock.acquire()  
      if (listaNumeri.getLen() == 0):  
          waitC.wait()  
      listaNumeri.stampa()  
      listaNumeri.svuota()  
      waitP.notify()  
      lock.release()  
        
  def produce(waitC, waitP, listaNumeri):  
      lock.acquire()  
      if (listaNumeri.getLen() &gt; 0):  
          waitP.wait()  
      for i in range(10):  
          listaNumeri.appendi(i)  
      waitC.notify()  
      lock.release()  
    
        
  def main():  
      mymanager = numManager()  
      mymanager.start()  
      listaNumeri = mymanager.numeri()  
      producer = Process(target = produce, args =(waitC, waitP, listaNumeri,))  
      producer.start()  
      time.sleep(2)  
      consumer = Process(target = consume, args =(waitC, waitP, listaNumeri,))  
      consumer.start()  
    
  main() 
  </code></pre>
  <p>Anyway it always crashes like that, telling me this:</p>
  <pre class="lang-none prettyprint-override"><code>Process Process-3:
  Traceback (most recent call last):
    File &quot;/usr/lib/python2.7/multiprocessing/process.py&quot;, line 258, in _bootstrap
      self.run()
    File &quot;/usr/lib/python2.7/multiprocessing/process.py&quot;, line 114, in run
      self._target(*self._args, **self._kwargs)
    File &quot;./trySemProc.py&quot;, line 61, in consume
      if (listaNumeri.getLen() == 0):
    File &quot;&lt;string&gt;&quot;, line 2, in getLen
    File &quot;/usr/lib/python2.7/multiprocessing/managers.py&quot;, line 755, in _callmethod
      self._connect()
    File &quot;/usr/lib/python2.7/multiprocessing/managers.py&quot;, line 742, in _connect
      conn = self._Client(self._token.address, authkey=self._authkey)
    File &quot;/usr/lib/python2.7/multiprocessing/connection.py&quot;, line 169, in Client
      c = SocketClient(address)
    File &quot;/usr/lib/python2.7/multiprocessing/connection.py&quot;, line 293, in SocketClient
      s.connect(address)
    File &quot;/usr/lib/python2.7/socket.py&quot;, line 224, in meth
      return getattr(self._sock,name)(*args)
  error: [Errno 2] No such file or directory
  </code></pre>
  <p>So what's the matter? How should I use these <code>managers</code> to share objects and their methods?</p>