2023-08-22 11:15:46 -04:00
|
|
|
# test asyncio.gather() function
|
2019-11-13 05:08:22 -05:00
|
|
|
|
|
|
|
try:
|
2023-08-22 11:15:46 -04:00
|
|
|
import asyncio
|
2019-11-13 05:08:22 -05:00
|
|
|
except ImportError:
|
2023-08-22 11:15:46 -04:00
|
|
|
print("SKIP")
|
|
|
|
raise SystemExit
|
2019-11-13 05:08:22 -05:00
|
|
|
|
|
|
|
|
2023-10-18 17:49:14 -04:00
|
|
|
# CIRCUITPY provides __await__()
|
2023-08-20 09:02:31 -04:00
|
|
|
async def foo():
|
|
|
|
return 42
|
|
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
foo().__await__
|
|
|
|
except AttributeError:
|
|
|
|
print("SKIP")
|
|
|
|
raise SystemExit
|
|
|
|
|
|
|
|
|
2019-11-13 05:08:22 -05:00
|
|
|
async def factorial(name, number):
|
|
|
|
f = 1
|
|
|
|
for i in range(2, number + 1):
|
|
|
|
print("Task {}: Compute factorial({})...".format(name, i))
|
|
|
|
await asyncio.sleep(0.01)
|
|
|
|
f *= i
|
|
|
|
print("Task {}: factorial({}) = {}".format(name, number, f))
|
|
|
|
return f
|
|
|
|
|
|
|
|
|
2022-05-03 08:39:58 -04:00
|
|
|
async def task(id, t=0.1):
|
2019-11-13 05:08:22 -05:00
|
|
|
print("start", id)
|
2022-04-20 23:19:34 -04:00
|
|
|
await asyncio.sleep(t)
|
2019-11-13 05:08:22 -05:00
|
|
|
print("end", id)
|
2021-11-16 21:45:42 -05:00
|
|
|
return id
|
2019-11-13 05:08:22 -05:00
|
|
|
|
|
|
|
|
2022-03-28 21:57:04 -04:00
|
|
|
async def task_loop(id):
|
|
|
|
print("task_loop start", id)
|
|
|
|
while True:
|
2022-05-03 08:39:58 -04:00
|
|
|
await asyncio.sleep(0.1)
|
2022-03-28 21:57:04 -04:00
|
|
|
print("task_loop loop", id)
|
|
|
|
|
|
|
|
|
2022-05-03 08:39:58 -04:00
|
|
|
async def task_raise(id, t=0.1):
|
2022-03-28 21:57:04 -04:00
|
|
|
print("task_raise start", id)
|
2022-04-20 03:11:55 -04:00
|
|
|
await asyncio.sleep(t)
|
|
|
|
print("task_raise raise", id)
|
2022-03-28 21:57:04 -04:00
|
|
|
raise ValueError(id)
|
|
|
|
|
|
|
|
|
|
|
|
async def gather_task(t0, t1):
|
2019-11-13 05:08:22 -05:00
|
|
|
print("gather_task")
|
2022-03-28 21:57:04 -04:00
|
|
|
await asyncio.gather(t0, t1)
|
2019-11-13 05:08:22 -05:00
|
|
|
print("gather_task2")
|
|
|
|
|
|
|
|
|
|
|
|
async def main():
|
|
|
|
# Simple gather with return values
|
|
|
|
print(await asyncio.gather(factorial("A", 2), factorial("B", 3), factorial("C", 4)))
|
|
|
|
|
2022-03-28 21:57:04 -04:00
|
|
|
print("====")
|
|
|
|
|
2022-07-20 19:54:02 -04:00
|
|
|
# Gather with no awaitables
|
|
|
|
print(await asyncio.gather())
|
|
|
|
|
|
|
|
print("====")
|
|
|
|
|
2021-11-16 21:45:42 -05:00
|
|
|
# Test return_exceptions, where one task is cancelled and the other finishes normally
|
|
|
|
tasks = [asyncio.create_task(task(1)), asyncio.create_task(task(2))]
|
|
|
|
tasks[0].cancel()
|
|
|
|
print(await asyncio.gather(*tasks, return_exceptions=True))
|
|
|
|
|
2022-03-28 21:57:04 -04:00
|
|
|
print("====")
|
|
|
|
|
|
|
|
# Test return_exceptions, where one task raises an exception and the other finishes normally.
|
|
|
|
tasks = [asyncio.create_task(task(1)), asyncio.create_task(task_raise(2))]
|
|
|
|
print(await asyncio.gather(*tasks, return_exceptions=True))
|
|
|
|
|
|
|
|
print("====")
|
|
|
|
|
|
|
|
# Test case where one task raises an exception and other task keeps running.
|
|
|
|
tasks = [asyncio.create_task(task_loop(1)), asyncio.create_task(task_raise(2))]
|
|
|
|
try:
|
|
|
|
await asyncio.gather(*tasks)
|
|
|
|
except ValueError as er:
|
|
|
|
print(repr(er))
|
|
|
|
print(tasks[0].done(), tasks[1].done())
|
|
|
|
for t in tasks:
|
|
|
|
t.cancel()
|
2022-05-03 08:39:58 -04:00
|
|
|
await asyncio.sleep(0.2)
|
2022-03-28 21:57:04 -04:00
|
|
|
|
|
|
|
print("====")
|
|
|
|
|
|
|
|
# Test case where both tasks raise an exception.
|
2022-04-20 03:11:55 -04:00
|
|
|
# Use t=0 so they raise one after the other, between the gather starting and finishing.
|
|
|
|
tasks = [asyncio.create_task(task_raise(1, t=0)), asyncio.create_task(task_raise(2, t=0))]
|
2022-03-28 21:57:04 -04:00
|
|
|
try:
|
|
|
|
await asyncio.gather(*tasks)
|
|
|
|
except ValueError as er:
|
|
|
|
print(repr(er))
|
|
|
|
print(tasks[0].done(), tasks[1].done())
|
|
|
|
|
|
|
|
print("====")
|
|
|
|
|
|
|
|
# Cancel a multi gather.
|
|
|
|
t = asyncio.create_task(gather_task(task(1), task(2)))
|
2022-05-03 08:39:58 -04:00
|
|
|
await asyncio.sleep(0.05)
|
2022-03-28 21:57:04 -04:00
|
|
|
t.cancel()
|
2022-05-03 08:39:58 -04:00
|
|
|
await asyncio.sleep(0.2)
|
2019-11-13 05:08:22 -05:00
|
|
|
|
2022-04-20 23:19:34 -04:00
|
|
|
# Test edge cases where the gather is cancelled just as tasks are created and ending.
|
|
|
|
for i in range(1, 4):
|
|
|
|
print("====")
|
|
|
|
t = asyncio.create_task(gather_task(task(1, t=0), task(2, t=0)))
|
|
|
|
for _ in range(i):
|
|
|
|
await asyncio.sleep(0)
|
|
|
|
t.cancel()
|
|
|
|
await asyncio.sleep(0.2)
|
|
|
|
|
2019-11-13 05:08:22 -05:00
|
|
|
|
|
|
|
asyncio.run(main())
|