-
-
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
base: master
Are you sure you want to change the base?
extmod/asyncio: Add Task
methods from CPython
#13000
Conversation
Code size report:
|
964a9a9
to
f029578
Compare
Task
methods to make _asyncio
more similar to cpythonTask
methods to make _asyncio
more similar to cpython
Task
methods to make _asyncio
more similar to cpythonTask
methods from CPython
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## master #13000 +/- ##
=======================================
Coverage 98.43% 98.43%
=======================================
Files 158 158
Lines 20978 21027 +49
=======================================
+ Hits 20649 20698 +49
Misses 329 329 ☔ View full report in Codecov by Sentry. |
da05a15
to
fb170d9
Compare
921984b
to
ae0905c
Compare
Adds methods that are in CPython, such as `exception`, `result`, `get_coro`, `cancelled`, `add_done_callback`, and `remove_done_callback`. Also adds support for the unary hash so tasks may be collected in a python set. Signed-off-by: James Ward <james@notjam.es>
Signed-off-by: James Ward <james@notjam.es>
Signed-off-by: James Ward <james@notjam.es>
ae0905c
to
e1ef6ed
Compare
I personally wouldn't worry about adding:
Adding even function names for these costs space, and not adding them will just raise an exception anyway, it's really not much different! |
I guess so. My concern was that the exception would be a different kind of error but that's probably ok. |
d65d495
to
a97ef0d
Compare
Signed-off-by: James Ward <james@notjam.es>
Signed-off-by: James Ward <james@notjam.es>
0d98d3b
to
db9db03
Compare
Signed-off-by: James Ward <james@notjam.es>
db9db03
to
d23edc3
Compare
Okay, dropped those functions and fixed the commit messages. |
Thanks for the contribution. MicroPython aims to be compact so it can run on devices without many resources. As such, these kinds of methods/functions were left out of the asyncio implementation on purpose (among many others). That said, we can add them if they are important and useful. What motivated you to add these particular features? Do they add anything that you can't already do? I don't think the done-callback functions are really needed. MicroPython doesn't support any kind of callback in asyncio. Instead you can wait on the task to complete then run the required code. |
|
||
def add_done_callback(self, callback): | ||
if not self.state: | ||
callback(self, self.data) |
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?
extmod/modasyncio.c
Outdated
@@ -255,6 +387,15 @@ STATIC void task_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) { | |||
} | |||
} | |||
|
|||
STATIC mp_obj_t task_unary_op(mp_unary_op_t op, mp_obj_t o_in) { |
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.
Hashing of Task should actually work, since commit eaccaa3. So I think this bit of code can be removed.
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'll confirm it without this, it's very likely that's the case!
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.
Yup, seems fine with that patch, awesome. Removed that change from this PR.
Definitely +1. |
Of course! :)
Compact is a great goal, and I do recognize that a lot of effort was put in to make asyncio as small as possible -- and also a lot of effort to make the compatibility with the CPython asyncio implementation as high as possible within that constraint.
All of these features expose existing "private" APIs (plumbing?) via the CPython public API (porcelain?). So everything can technically be done today - but in a way that's more brittle. In particular, this PR heavily simplifies understanding if a task is cancelled or not, and if it's "done" what the result of it running was. It's not documented anywhere how the The use case I had for this was to implement the The example CPython gives for tasks groups is as such:
Where the
I had added them because micropython kind of supports them via
If you |
Signed-off-by: James Ward <james@notjam.es>
d8459c3
to
8d260d8
Compare
For task groups see #8791. A very useful enhancement. |
This is an automated heads-up that we've just merged a Pull Request See #13763 A search suggests this PR might apply the STATIC macro to some C code. If it Although this is an automated message, feel free to @-reply to me directly if |
This updates the way that Task is defined in the C module for asyncio which implements tasks & task queues. The goal is to bring
asyncio
in MicroPython slightly closer to CPython.This adds the following methods to
Task
:get_coro()
- how CPython exposes the coroutine backing the task (CPython Docs)result()
- a helper method from CPython for returning the successful response (CPython Docs)exception()
- a helper method from CPython for returning the raised values (CPython Docs)cancelled()
- helper for true / false if the task has been cancelled (CPython Docs)add_done_callback()
- adds a callback to the task for completion or if already complete executes it (CPython docs)remove_done_callback()
- removes a specific "done" callback from the task (CPython docs)Adds ahash
unary operation soTask
s can be added to sets like in CPython.Also addsset_result
andset_exception
for consistency but neither of these do anything - they raise aRuntimeError
because they aren't supported on Tasks. 🤷