Skip to content

pmap should support multiple iterables #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions gbatchy/utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from collections import deque, OrderedDict
from gevent import Timeout, iwait as _gevent_iwait, wait as _gevent_wait, pool as _gevent_pool, queue as _gevent_queue, sleep
from itertools import izip_longest
try:
from peak.util.proxies import LazyProxy
except ImportError:
Expand Down Expand Up @@ -32,8 +33,8 @@ def pget(lst):
return [x.get() for x in lst]

@batch_context
def pmap(fn, items, **kwargs):
return pget(spawn(fn, i, **kwargs) for i in items)
def pmap(fn, *seqs, **kwargs):
return pget(spawn(fn, *args, **kwargs) for args in izip_longest(*seqs))

@batch_context
def pmap_unordered(fn, items, **kwargs):
Expand Down Expand Up @@ -433,4 +434,4 @@ def pmap(self, *args, **kwargs):
return list(self.imap(*args, **kwargs))
map = pmap


6 changes: 6 additions & 0 deletions tests/batch_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,10 @@ def test():
test() # shouldn't hang.

def test_utils(self):
@batched()
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one shouldn't be batched.

def add(x, y):
return x + y

@batched()
def add_n(args_list):
return [args[0] + kwargs.get('n', 1)
Expand All @@ -202,6 +206,8 @@ def test():
self.assertEquals([3,4,5], pmap(add_n, [1,2,3], n=2))
self.assertEquals([2], pfilter(only_even, [1,2,3]))

self.assertEquals([11,13,23], pmap([7,11,10], [4,2,13]))

self.assertEquals([2,3,4], sorted(pmap_unordered(add_n, [1,2,3])))
self.assertEquals([3,4,5], sorted(pmap_unordered(add_n, [1,2,3], n=2)))
self.assertEquals([2], list(pfilter_unordered(only_even, [1,2,3])))
Expand Down