6464def mapstar (args ):
6565 return list (map (* args ))
6666
67+ def starmapstar (args ):
68+ return list (itertools .starmap (args [0 ], args [1 ]))
69+
6770#
6871# Code run by worker processes
6972#
@@ -248,7 +251,25 @@ def map(self, func, iterable, chunksize=None):
248251 in a list that is returned.
249252 '''
250253 assert self ._state == RUN
251- return self .map_async (func , iterable , chunksize ).get ()
254+ return self ._map_async (func , iterable , mapstar , chunksize ).get ()
255+
256+ def starmap (self , func , iterable , chunksize = None ):
257+ '''
258+ Like `map()` method but the elements of the `iterable` are expected to
259+ be iterables as well and will be unpacked as arguments. Hence
260+ `func` and (a, b) becomes func(a, b).
261+ '''
262+ assert self ._state == RUN
263+ return self ._map_async (func , iterable , starmapstar , chunksize ).get ()
264+
265+ def starmap_async (self , func , iterable , chunksize = None , callback = None ,
266+ error_callback = None ):
267+ '''
268+ Asynchronous version of `starmap()` method.
269+ '''
270+ assert self ._state == RUN
271+ return self ._map_async (func , iterable , starmapstar , chunksize ,
272+ callback , error_callback )
252273
253274 def imap (self , func , iterable , chunksize = 1 ):
254275 '''
@@ -302,6 +323,13 @@ def map_async(self, func, iterable, chunksize=None, callback=None,
302323 Asynchronous version of `map()` method.
303324 '''
304325 assert self ._state == RUN
326+ return self ._map_async (func , iterable , mapstar , chunksize )
327+
328+ def _map_async (self , func , iterable , mapper , chunksize = None , callback = None ,
329+ error_callback = None ):
330+ '''
331+ Helper function to implement map, starmap and their async counterparts.
332+ '''
305333 if not hasattr (iterable , '__len__' ):
306334 iterable = list (iterable )
307335
@@ -315,7 +343,7 @@ def map_async(self, func, iterable, chunksize=None, callback=None,
315343 task_batches = Pool ._get_tasks (func , iterable , chunksize )
316344 result = MapResult (self ._cache , chunksize , len (iterable ), callback ,
317345 error_callback = error_callback )
318- self ._taskqueue .put ((((result ._job , i , mapstar , (x ,), {})
346+ self ._taskqueue .put ((((result ._job , i , mapper , (x ,), {})
319347 for i , x in enumerate (task_batches )), None ))
320348 return result
321349
0 commit comments