[Python-checkins] cpython: raise a ValueError instead of an AssertionError when pool is an invalid state

benjamin.peterson python-checkins at python.org
Tue Sep 25 18:46:00 CEST 2012


http://hg.python.org/cpython/rev/d24b996d136e
changeset:   79179:d24b996d136e
parent:      79177:78a6e9ecba0b
user:        Benjamin Peterson <benjamin at python.org>
date:        Tue Sep 25 12:45:42 2012 -0400
summary:
  raise a ValueError instead of an AssertionError when pool is an invalid state

files:
  Lib/multiprocessing/pool.py      |  15 ++++++++-------
  Lib/test/test_multiprocessing.py |   3 ++-
  2 files changed, 10 insertions(+), 8 deletions(-)


diff --git a/Lib/multiprocessing/pool.py b/Lib/multiprocessing/pool.py
--- a/Lib/multiprocessing/pool.py
+++ b/Lib/multiprocessing/pool.py
@@ -225,7 +225,6 @@
         Apply `func` to each element in `iterable`, collecting the results
         in a list that is returned.
         '''
-        assert self._state == RUN
         return self._map_async(func, iterable, mapstar, chunksize).get()
 
     def starmap(self, func, iterable, chunksize=None):
@@ -234,7 +233,6 @@
         be iterables as well and will be unpacked as arguments. Hence
         `func` and (a, b) becomes func(a, b).
         '''
-        assert self._state == RUN
         return self._map_async(func, iterable, starmapstar, chunksize).get()
 
     def starmap_async(self, func, iterable, chunksize=None, callback=None,
@@ -242,7 +240,6 @@
         '''
         Asynchronous version of `starmap()` method.
         '''
-        assert self._state == RUN
         return self._map_async(func, iterable, starmapstar, chunksize,
                                callback, error_callback)
 
@@ -250,7 +247,8 @@
         '''
         Equivalent of `map()` -- can be MUCH slower than `Pool.map()`.
         '''
-        assert self._state == RUN
+        if self._state != RUN:
+            raise ValueError("Pool not running")
         if chunksize == 1:
             result = IMapIterator(self._cache)
             self._taskqueue.put((((result._job, i, func, (x,), {})
@@ -268,7 +266,8 @@
         '''
         Like `imap()` method but ordering of results is arbitrary.
         '''
-        assert self._state == RUN
+        if self._state != RUN:
+            raise ValueError("Pool not running")
         if chunksize == 1:
             result = IMapUnorderedIterator(self._cache)
             self._taskqueue.put((((result._job, i, func, (x,), {})
@@ -287,7 +286,8 @@
         '''
         Asynchronous version of `apply()` method.
         '''
-        assert self._state == RUN
+        if self._state != RUN:
+            raise ValueError("Pool not running")
         result = ApplyResult(self._cache, callback, error_callback)
         self._taskqueue.put(([(result._job, None, func, args, kwds)], None))
         return result
@@ -297,7 +297,6 @@
         '''
         Asynchronous version of `map()` method.
         '''
-        assert self._state == RUN
         return self._map_async(func, iterable, mapstar, chunksize)
 
     def _map_async(self, func, iterable, mapper, chunksize=None, callback=None,
@@ -305,6 +304,8 @@
         '''
         Helper function to implement map, starmap and their async counterparts.
         '''
+        if self._state != RUN:
+            raise ValueError("Pool not running")
         if not hasattr(iterable, '__len__'):
             iterable = list(iterable)
 
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
@@ -1727,7 +1727,8 @@
             with multiprocessing.Pool(2) as p:
                 r = p.map_async(sqr, L)
                 self.assertEqual(r.get(), expected)
-            self.assertRaises(AssertionError, p.map_async, sqr, L)
+            print(p._state)
+            self.assertRaises(ValueError, p.map_async, sqr, L)
 
 def raising():
     raise KeyError("key")

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


More information about the Python-checkins mailing list