forked from adafruit/circuitpython
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
extmod/uasyncio: Fix gather cancelling and handling of exceptions.
The following fixes are made: - cancelling a gather now cancels all sub-tasks of the gather (previously it would only cancel the first) - if any sub-task of a gather raises an exception then the gather finishes (previously it would only finish if the first sub-task raised) Fixes issues adafruit#5798, adafruit#7807, adafruit#7901. Signed-off-by: Damien George <damien@micropython.org>
- Loading branch information
Showing
5 changed files
with
202 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
# Test uasyncio.gather() function, features that are not implemented. | ||
|
||
try: | ||
import uasyncio as asyncio | ||
except ImportError: | ||
try: | ||
import asyncio | ||
except ImportError: | ||
print("SKIP") | ||
raise SystemExit | ||
|
||
|
||
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()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
task start 1 | ||
task start 2 | ||
gather_task start | ||
RuntimeError("can't wait",) | ||
task end 1 | ||
task end 2 | ||
gather_task end | ||
==== | ||
task start 1 | ||
task start 2 | ||
gather_task start | ||
RuntimeError("can't gather",) | ||
task end 1 | ||
task end 2 |