Skip to content

Commit 175dd2b

Browse files
author
czheo
committed
BUGFIX: multi green thread with one item
1 parent 04bf3da commit 175dd2b

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

syntax_sugar/pipe.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,11 @@ def multithread(fn, poolsize, data):
7474

7575
def multigreenthread(fn, poolsize, data):
7676
p = GreenPool(poolsize)
77-
return list(p.imap(fn, data))
77+
if not hasattr(data, '__iter__'):
78+
data= [data]
79+
return next(p.imap(fn, data))
80+
else:
81+
return list(p.imap(fn, data))
7882

7983

8084
process_syntax = p = ProcessSyntax()

tests/test_pipe.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,20 @@ def test_pipe_connect():
3030
# not all processes in the pool are necessarily used
3131
#
3232
def test_pipe_multiprocess():
33+
assert pipe(10) | p[lambda x: x**2] | END == 10**2
34+
assert pipe(10) | p[lambda x: x**2] * 2 | END == 10**2
3335
assert pipe(100) | range | p[lambda x: x**2] * 3 | sorted | END == [x ** 2 for x in range(100)]
3436

3537
def test_pipe_multithread():
38+
assert pipe(10) | t[lambda x: x**2] | END == 10**2
39+
assert pipe(10) | t[lambda x: x**2] * 2 | END == 10**2
3640
assert pipe(100) | range | t[lambda x: x**2] * 3 | sorted | END == [x ** 2 for x in range(100)]
3741

3842
def test_pipe_multigreenthread():
43+
assert pipe(10) | [lambda x: x**2] | END == 10**2
44+
assert pipe(10) | [lambda x: x**2] * 2 | END == 10**2
45+
assert pipe(10) | g[lambda x: x**2] | END == 10**2
46+
assert pipe(10) | g[lambda x: x**2] * 2 | END == 10**2
3947
assert pipe(10000) | range | [lambda x: x**2] * 10000 | sorted | END == [x ** 2 for x in range(10000)]
4048
assert pipe(10000) | range | g[lambda x: x**2] * 10000 | sorted | END == [x ** 2 for x in range(10000)]
4149

0 commit comments

Comments
 (0)