-
-
Notifications
You must be signed in to change notification settings - Fork 8.2k
extmod/asyncio: Add Task
methods from CPython
#13000
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
imnotjames
wants to merge
7
commits into
micropython:master
Choose a base branch
from
imnotjames:feat/upython/async-tasks
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+640
−0
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
bc944ec
extmod/modasyncio.c: Add Task methods to get tasks closer to CPython.
imnotjames df39e51
extmod/asyncio: Add cpython task methods.
imnotjames e1ef6ed
tests/extmod: Add asyncio tests for new task features.
imnotjames 5ecb859
tests/extmod: Drop set_result and set_exception tests.
imnotjames 92092f0
extmod/modasyncio.c: Drop set_result and set_exception.
imnotjames d23edc3
extmod/asyncio: Drop set_exception and set_result.
imnotjames 8d260d8
extmod/modasyncio.c: Rely on the generic hash during runtime.
imnotjames File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,54 @@ | ||
# Test the Task.add_done_callback() method | ||
|
||
try: | ||
import asyncio | ||
except ImportError: | ||
print("SKIP") | ||
raise SystemExit | ||
|
||
|
||
async def task(t, exc=None): | ||
if t >= 0: | ||
await asyncio.sleep(t) | ||
if exc: | ||
raise exc | ||
|
||
|
||
def done_callback(t, er): | ||
print("done", repr(t), repr(er)) | ||
|
||
|
||
async def main(): | ||
# Tasks that aren't done only execute done callback after finishing | ||
print("=" * 10) | ||
t = asyncio.create_task(task(-1)) | ||
t.add_done_callback(done_callback) | ||
print("Waiting for task to complete") | ||
await asyncio.sleep(0) | ||
print("Task has completed") | ||
|
||
# Task that are done run the callback immediately | ||
print("=" * 10) | ||
t = asyncio.create_task(task(-1)) | ||
await asyncio.sleep(0) | ||
print("Task has completed") | ||
t.add_done_callback(done_callback) | ||
print("Callback Added") | ||
|
||
# Task that starts, runs and finishes without an exception should return None | ||
print("=" * 10) | ||
t = asyncio.create_task(task(0.01)) | ||
t.add_done_callback(done_callback) | ||
try: | ||
t.add_done_callback(done_callback) | ||
except RuntimeError as e: | ||
print("Second call to add_done_callback emits error:", repr(e)) | ||
|
||
# Task that raises immediately should still run done callback | ||
print("=" * 10) | ||
t = asyncio.create_task(task(-1, ValueError)) | ||
t.add_done_callback(done_callback) | ||
await asyncio.sleep(0) | ||
|
||
|
||
asyncio.run(main()) |
This file contains hidden or 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,12 @@ | ||
========== | ||
Waiting for task to complete | ||
done <Task> StopIteration() | ||
Task has completed | ||
========== | ||
Task has completed | ||
done <Task> StopIteration() | ||
Callback Added | ||
========== | ||
Second call to add_done_callback emits error: RuntimeError('>1 callback unsupported',) | ||
========== | ||
done <Task> ValueError() |
This file contains hidden or 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,54 @@ | ||
# Test the `Task.cancelled` method | ||
|
||
try: | ||
import asyncio | ||
except ImportError: | ||
print("SKIP") | ||
raise SystemExit | ||
|
||
|
||
async def task(t): | ||
await asyncio.sleep(t) | ||
|
||
|
||
async def main(): | ||
# Cancel task immediately doesn't mark the task as cancelled | ||
print("=" * 10) | ||
t = asyncio.create_task(task(2)) | ||
t.cancel() | ||
print("Expecting task to not be cancelled because it is not done:", t.cancelled()) | ||
|
||
# Cancel task immediately and wait for cancellation to complete | ||
print("=" * 10) | ||
t = asyncio.create_task(task(2)) | ||
t.cancel() | ||
await asyncio.sleep(0) | ||
print("Expecting Task to be Cancelled:", t.cancelled()) | ||
|
||
# Cancel task and wait for cancellation to complete | ||
print("=" * 10) | ||
t = asyncio.create_task(task(2)) | ||
await asyncio.sleep(0.01) | ||
t.cancel() | ||
await asyncio.sleep(0) | ||
print("Expecting Task to be Cancelled:", t.cancelled()) | ||
|
||
# Cancel task multiple times after it has started | ||
print("=" * 10) | ||
t = asyncio.create_task(task(2)) | ||
await asyncio.sleep(0.01) | ||
for _ in range(4): | ||
t.cancel() | ||
await asyncio.sleep(0.01) | ||
|
||
print("Expecting Task to be Cancelled:", t.cancelled()) | ||
|
||
# Cancel task after it has finished | ||
print("=" * 10) | ||
t = asyncio.create_task(task(0.01)) | ||
await asyncio.sleep(0.05) | ||
t.cancel() | ||
print("Expecting task to not be Cancelled:", t.cancelled()) | ||
|
||
|
||
asyncio.run(main()) |
This file contains hidden or 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,10 @@ | ||
========== | ||
Expecting task to not be cancelled because it is not done: False | ||
========== | ||
Expecting Task to be Cancelled: True | ||
========== | ||
Expecting Task to be Cancelled: True | ||
========== | ||
Expecting Task to be Cancelled: True | ||
========== | ||
Expecting task to not be Cancelled: False |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This signature of the callback does not match CPython. In CPython the callback takes only one argument.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's right - I'm hoping to follow up to bring this in line with CPython in cases where only one argument is accepted in the function in a follow up PR.
However, changing the callback signature here would mean it's different from where it gets called elsewhere.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I could add a comment to clarify that if it helps?