2023-06-08 02:01:38 -04:00
|
|
|
# Test asyncio.gather() function, features that are not implemented.
|
2022-03-28 21:57:04 -04:00
|
|
|
|
|
|
|
try:
|
2023-08-22 11:15:46 -04:00
|
|
|
import asyncio
|
2022-03-28 21:57:04 -04:00
|
|
|
except ImportError:
|
2023-08-22 11:15:46 -04:00
|
|
|
print("SKIP")
|
|
|
|
raise SystemExit
|
2022-03-28 21:57:04 -04:00
|
|
|
|
|
|
|
|
2023-10-19 16:42:36 -04:00
|
|
|
# CIRCUITPY-CHANGE: CircuitPython provides __await__()
|
2023-08-20 09:02:31 -04:00
|
|
|
async def foo():
|
|
|
|
return 42
|
|
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
foo().__await__
|
|
|
|
except AttributeError:
|
|
|
|
print("SKIP")
|
|
|
|
raise SystemExit
|
|
|
|
|
|
|
|
|
2022-03-28 21:57:04 -04:00
|
|
|
def custom_handler(loop, context):
|
|
|
|
print(repr(context["exception"]))
|
|
|
|
|
|
|
|
|
|
|
|
async def task(id):
|
|
|
|
print("task start", id)
|
|
|
|
await asyncio.sleep(0.01)
|
|
|
|
print("task end", id)
|
|
|
|
return id
|
|
|
|
|
|
|
|
|
|
|
|
async def gather_task(t0, t1):
|
|
|
|
print("gather_task start")
|
|
|
|
await asyncio.gather(t0, t1)
|
|
|
|
print("gather_task end")
|
|
|
|
|
|
|
|
|
|
|
|
async def main():
|
|
|
|
loop = asyncio.get_event_loop()
|
|
|
|
loop.set_exception_handler(custom_handler)
|
|
|
|
|
|
|
|
# Test case where can't wait on a task being gathered.
|
|
|
|
tasks = [asyncio.create_task(task(1)), asyncio.create_task(task(2))]
|
|
|
|
gt = asyncio.create_task(gather_task(tasks[0], tasks[1]))
|
|
|
|
await asyncio.sleep(0) # let the gather start
|
|
|
|
try:
|
|
|
|
await tasks[0] # can't await because this task is part of the gather
|
|
|
|
except RuntimeError as er:
|
|
|
|
print(repr(er))
|
|
|
|
await gt
|
|
|
|
|
|
|
|
print("====")
|
|
|
|
|
|
|
|
# Test case where can't gather on a task being waited.
|
|
|
|
tasks = [asyncio.create_task(task(1)), asyncio.create_task(task(2))]
|
|
|
|
asyncio.create_task(gather_task(tasks[0], tasks[1]))
|
|
|
|
await tasks[0] # wait on this task before the gather starts
|
|
|
|
await tasks[1]
|
|
|
|
|
|
|
|
|
|
|
|
asyncio.run(main())
|