]> granicus.if.org Git - python/commitdiff
Issue #15064: Make BaseManager.__enter__() start server if necessary.
authorRichard Oudkerk <shibturn@gmail.com>
Mon, 18 Jun 2012 20:29:30 +0000 (21:29 +0100)
committerRichard Oudkerk <shibturn@gmail.com>
Mon, 18 Jun 2012 20:29:30 +0000 (21:29 +0100)
Doc/library/multiprocessing.rst
Lib/multiprocessing/managers.py
Lib/test/test_multiprocessing.py

index 2f64bb1f9271c416317c1723e0480e75ac83416f..885885915b885d442ad550611011f277a377a3e5 100644 (file)
@@ -1281,9 +1281,14 @@ their parent process exits.  The manager classes are defined in the
 
       The address used by the manager.
 
-   Manager objects support the context manager protocol -- see
-   :ref:`typecontextmanager`.  :meth:`__enter__` returns the
-   manager object, and :meth:`__exit__` calls :meth:`shutdown`.
+   .. versionchanged:: 3.3
+      Manager objects support the context manager protocol -- see
+      :ref:`typecontextmanager`.  :meth:`__enter__` starts the server
+      process (if it has not already started) and then returns the
+      manager object.  :meth:`__exit__` calls :meth:`shutdown`.
+
+      In previous versions :meth:`__enter__` did not start the
+      manager's server process if it was not already started.
 
 .. class:: SyncManager
 
index f6611af9a6cb67d674539081facceac59854c905..1ab147e29e6ca10d95cc85fbbced067a6b3bba1c 100644 (file)
@@ -561,6 +561,9 @@ class BaseManager(object):
             conn.close()
 
     def __enter__(self):
+        if self._state.value == State.INITIAL:
+            self.start()
+        assert self._state.value == State.STARTED
         return self
 
     def __exit__(self, exc_type, exc_val, exc_tb):
index 017e6b4796deb99cf4c630ff2afb35ee6d153fc4..f6f4f73952dd79a95e6cba22565769fe247b37a7 100644 (file)
@@ -1888,7 +1888,27 @@ class _TestMyManager(BaseTestCase):
     def test_mymanager(self):
         manager = MyManager()
         manager.start()
+        self.common(manager)
+        manager.shutdown()
+
+        # If the manager process exited cleanly then the exitcode
+        # will be zero.  Otherwise (after a short timeout)
+        # terminate() is used, resulting in an exitcode of -SIGTERM.
+        self.assertEqual(manager._process.exitcode, 0)
+
+    def test_mymanager_context(self):
+        with MyManager() as manager:
+            self.common(manager)
+        self.assertEqual(manager._process.exitcode, 0)
+
+    def test_mymanager_context_prestarted(self):
+        manager = MyManager()
+        manager.start()
+        with manager:
+            self.common(manager)
+        self.assertEqual(manager._process.exitcode, 0)
 
+    def common(self, manager):
         foo = manager.Foo()
         bar = manager.Bar()
         baz = manager.baz()
@@ -1911,12 +1931,6 @@ class _TestMyManager(BaseTestCase):
 
         self.assertEqual(list(baz), [i*i for i in range(10)])
 
-        manager.shutdown()
-
-        # If the manager process exited cleanly then the exitcode
-        # will be zero.  Otherwise (after a short timeout)
-        # terminate() is used, resulting in an exitcode of -SIGTERM.
-        self.assertEqual(manager._process.exitcode, 0)
 
 #
 # Test of connecting to a remote server and using xmlrpclib for serialization