From e57a1e4f3e56f517b48f07f7f02d2c6db5019467 Mon Sep 17 00:00:00 2001 From: Joungjin Lee Date: Wed, 13 Mar 2024 12:29:53 +0900 Subject: [PATCH] asynq_to_async: handle edge case - empty list of tasks --- asynq/asynq_to_async.py | 3 +++ asynq/tests/test_asynq_to_async.py | 6 ++++++ 2 files changed, 9 insertions(+) diff --git a/asynq/asynq_to_async.py b/asynq/asynq_to_async.py index ce8aa37..6562cb8 100644 --- a/asynq/asynq_to_async.py +++ b/asynq/asynq_to_async.py @@ -30,6 +30,9 @@ def is_asyncio_mode(): async def _gather(awaitables): """Gather awaitables, but wait all other awaitables to finish even if some of them fail.""" + if len(awaitables) == 0: + return [] + tasks = [asyncio.ensure_future(awaitable) for awaitable in awaitables] # Wait for all tasks to finish, even if some of them fail. diff --git a/asynq/tests/test_asynq_to_async.py b/asynq/tests/test_asynq_to_async.py index df2f32e..c5b73d3 100644 --- a/asynq/tests/test_asynq_to_async.py +++ b/asynq/tests/test_asynq_to_async.py @@ -51,6 +51,12 @@ def g(x): assert asyncio.run(g.asyncio(5)) == {"a": [1, 2], "b": (3, 4), "c": 5, "d": 200} + @asynq.asynq() + def empty(): + return (yield []) + + assert asyncio.run(empty.asyncio()) == [] + def test_asyncio_exception(): call_count = 0