title: Stopping a Python Multiprocessing BaseManager 'serve_forever' server?
body: <p>I have the following setup in Python 3.6 for interprocess communication:</p>
  
  <pre><code>from multiprocessing.managers import BaseManager
  class MyManager(BaseManager): pass
  MyManager.register('get_instance', callable=lambda:my_instance)
  m = MyManager(address=('', 50000), authkey=b'key')
  s = m.get_server()
  s.serve_forever()
  </code></pre>
  
  <p>To avoid blocking my message loop of a larger application, I used a thread to contain this setup. So <code>s.serve_forever()</code> actually runs inside a <code>run</code> function of a thread.</p>
  
  <p>This is done according to the <a href="https://docs.python.org/3.6/library/multiprocessing.html#managers" rel="noreferrer">documentation</a>. And the setup itself worked fine with client managers calling into the shared instance.</p>
  
  <p>However, I found no way to stop this 'serve_forever' server. Since the documentation didn't mention this, I checked the <a href="https://github.com/python/cpython/blob/3.6/Lib/multiprocessing/managers.py" rel="noreferrer">source code</a>. There's a <code>stop_event</code> which supposedly I can <code>set</code> to quit the loop. But it didn't work, as the <code>accepter</code> daemon/thread is still running. And I can't call <code>shutdown</code> on the server object because I don't have the socket object <code>c</code>.</p>
  
  <p>So how do I shut down this server?</p>
  
  <p>ps. Using <code>BaseManager.start()</code> isn't really an option for me because the server in my case shares an async message loop which can only be accessed by the starting process. <code>BaseManager.start()</code> spawns a new process which no longer has access to the message loop. <code>get_server().serve_forever()</code> on the other hand, runs within the calling process.</p>