[Python-checkins] cpython: Issue #15064: Make BaseManager.__enter__() start server if necessary.

richard.oudkerk python-checkins at python.org
Mon Jun 18 22:31:52 CEST 2012


http://hg.python.org/cpython/rev/198382b4bcd0
changeset:   77511:198382b4bcd0
user:        Richard Oudkerk <shibturn at gmail.com>
date:        Mon Jun 18 21:29:30 2012 +0100
summary:
  Issue #15064: Make BaseManager.__enter__() start server if necessary.

files:
  Doc/library/multiprocessing.rst  |  11 +++++--
  Lib/multiprocessing/managers.py  |   3 ++
  Lib/test/test_multiprocessing.py |  28 +++++++++++++++-----
  3 files changed, 32 insertions(+), 10 deletions(-)


diff --git a/Doc/library/multiprocessing.rst b/Doc/library/multiprocessing.rst
--- a/Doc/library/multiprocessing.rst
+++ b/Doc/library/multiprocessing.rst
@@ -1281,9 +1281,14 @@
 
       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
 
diff --git a/Lib/multiprocessing/managers.py b/Lib/multiprocessing/managers.py
--- a/Lib/multiprocessing/managers.py
+++ b/Lib/multiprocessing/managers.py
@@ -561,6 +561,9 @@
             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):
diff --git a/Lib/test/test_multiprocessing.py b/Lib/test/test_multiprocessing.py
--- a/Lib/test/test_multiprocessing.py
+++ b/Lib/test/test_multiprocessing.py
@@ -1888,7 +1888,27 @@
     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 @@
 
         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

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list