cpython: b07b1e58582d (original) (raw)

Mercurial > cpython

changeset 74107:b07b1e58582d

Issue #12708: Add starmap() and starmap_async() methods (similar to itertools.starmap()) to multiprocessing.Pool. Patch by Hynek Schlawack. [#12708]

Antoine Pitrou solipsis@pitrou.net
date Wed, 21 Dec 2011 11:03:24 +0100
parents b08bf8df8eec
children 1ab124a6f171
files Doc/library/multiprocessing.rst Lib/multiprocessing/managers.py Lib/multiprocessing/pool.py Lib/test/test_multiprocessing.py Misc/ACKS Misc/NEWS
diffstat 6 files changed, 72 insertions(+), 3 deletions(-)[+] [-] Doc/library/multiprocessing.rst 18 Lib/multiprocessing/managers.py 3 Lib/multiprocessing/pool.py 32 Lib/test/test_multiprocessing.py 18 Misc/ACKS 1 Misc/NEWS 3

line wrap: on

line diff

--- a/Doc/library/multiprocessing.rst +++ b/Doc/library/multiprocessing.rst @@ -1669,6 +1669,24 @@ with the :class:Pool class. returned iterator should be considered arbitrary. (Only when there is only one worker process is the order guaranteed to be "correct".)

+

+

+

+

+ .. method:: close() Prevents any more tasks from being submitted to the pool. Once all the

--- a/Lib/multiprocessing/managers.py +++ b/Lib/multiprocessing/managers.py @@ -1066,11 +1066,12 @@ ArrayProxy = MakeProxyType('ArrayProxy', PoolProxy = MakeProxyType('PoolProxy', ( 'apply', 'apply_async', 'close', 'imap', 'imap_unordered', 'join',

--- a/Lib/multiprocessing/pool.py +++ b/Lib/multiprocessing/pool.py @@ -64,6 +64,9 @@ job_counter = itertools.count() def mapstar(args): return list(map(*args)) +def starmapstar(args):

+ #

Code run by worker processes

# @@ -248,7 +251,25 @@ class Pool(object): in a list that is returned. ''' assert self._state == RUN

+

+

def imap(self, func, iterable, chunksize=1): ''' @@ -302,6 +323,13 @@ class Pool(object): Asynchronous version of map() method. ''' assert self._state == RUN

+

@@ -315,7 +343,7 @@ class Pool(object): task_batches = Pool._get_tasks(func, iterable, chunksize) result = MapResult(self._cache, chunksize, len(iterable), callback, error_callback=error_callback)

--- a/Lib/test/test_multiprocessing.py +++ b/Lib/test/test_multiprocessing.py @@ -8,6 +8,7 @@ import unittest import queue as pyqueue import time import io +import itertools import sys import os import gc @@ -1125,6 +1126,9 @@ def sqr(x, wait=0.0): time.sleep(wait) return x*x +def mul(x, y):

+ class _TestPool(BaseTestCase): def test_apply(self): @@ -1138,6 +1142,20 @@ class _TestPool(BaseTestCase): self.assertEqual(pmap(sqr, list(range(100)), chunksize=20), list(map(sqr, list(range(100)))))

+

+ def test_map_chunksize(self): try: self.pool.map_async(sqr, [], chunksize=1).get(timeout=TIMEOUT1)

--- a/Misc/ACKS +++ b/Misc/ACKS @@ -878,6 +878,7 @@ Michael Scharf Andreas Schawo Neil Schemenauer David Scherer +Hynek Schlawack Bob Schmertz Gregor Schmid Ralf Schmitt

--- a/Misc/NEWS +++ b/Misc/NEWS @@ -419,6 +419,9 @@ Core and Builtins Library ------- +- Issue #12708: Add starmap() and starmap_async() methods (similar to