@@ -261,6 +261,10 @@ def _setup_queues(self):
261261 self ._quick_put = self ._inqueue ._writer .send
262262 self ._quick_get = self ._outqueue ._reader .recv
263263
264+ def _check_running (self ):
265+ if self ._state != RUN :
266+ raise ValueError ("Pool not running" )
267+
264268 def apply (self , func , args = (), kwds = {}):
265269 '''
266270 Equivalent of `func(*args, **kwds)`.
@@ -306,8 +310,7 @@ def imap(self, func, iterable, chunksize=1):
306310 '''
307311 Equivalent of `map()` -- can be MUCH slower than `Pool.map()`.
308312 '''
309- if self ._state != RUN :
310- raise ValueError ("Pool not running" )
313+ self ._check_running ()
311314 if chunksize == 1 :
312315 result = IMapIterator (self ._cache )
313316 self ._taskqueue .put (
@@ -336,8 +339,7 @@ def imap_unordered(self, func, iterable, chunksize=1):
336339 '''
337340 Like `imap()` method but ordering of results is arbitrary.
338341 '''
339- if self ._state != RUN :
340- raise ValueError ("Pool not running" )
342+ self ._check_running ()
341343 if chunksize == 1 :
342344 result = IMapUnorderedIterator (self ._cache )
343345 self ._taskqueue .put (
@@ -366,8 +368,7 @@ def apply_async(self, func, args=(), kwds={}, callback=None,
366368 '''
367369 Asynchronous version of `apply()` method.
368370 '''
369- if self ._state != RUN :
370- raise ValueError ("Pool not running" )
371+ self ._check_running ()
371372 result = ApplyResult (self ._cache , callback , error_callback )
372373 self ._taskqueue .put (([(result ._job , 0 , func , args , kwds )], None ))
373374 return result
@@ -385,8 +386,7 @@ def _map_async(self, func, iterable, mapper, chunksize=None, callback=None,
385386 '''
386387 Helper function to implement map, starmap and their async counterparts.
387388 '''
388- if self ._state != RUN :
389- raise ValueError ("Pool not running" )
389+ self ._check_running ()
390390 if not hasattr (iterable , '__len__' ):
391391 iterable = list (iterable )
392392
@@ -625,6 +625,7 @@ def _terminate_pool(cls, taskqueue, inqueue, outqueue, pool,
625625 p .join ()
626626
627627 def __enter__ (self ):
628+ self ._check_running ()
628629 return self
629630
630631 def __exit__ (self , exc_type , exc_val , exc_tb ):
0 commit comments