Skip to content

Do not fail on mapping empty collections #13

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
3 changes: 3 additions & 0 deletions asyncio_pool/base_pool.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,9 @@ async def map(self, fn, iterable, cb=None, ctx=None, *,
fut = await self.spawn(fn(it), cb, ctx)
futures.append(fut)

if not futures:
return []

await aio.wait(futures)
return [get_result(fut) for fut in futures]

Expand Down
19 changes: 19 additions & 0 deletions tests/test_map.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@ async def test_map_simple():
assert res == [i*10 for i in task]


@pytest.mark.asyncio
async def test_map_empty():
task = []
pool = AioPool(size=7)
res = await pool.map(wrk, task)
assert res == []


@pytest.mark.asyncio
async def test_map_crash():
task = range(5)
Expand Down Expand Up @@ -52,6 +60,17 @@ async def wrk(n):
i += 1 # does not support enumerate btw (


@pytest.mark.asyncio
async def test_itermap_empty():
async def wrk(n):
await aio.sleep(n)
return n

async with AioPool(size=3) as pool:
async for res in pool.itermap(wrk, [], flat=False, timeout=0.6):
assert False


@pytest.mark.asyncio
async def test_itermap_cancel():

Expand Down