Skip to content
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

feat: add Task methods to make _asyncio more similar to cpython #8647

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

imnotjames
Copy link

This updates the way that Task is handled in the C module for asyncio which implements tasks & task queues. The goal is to bring asyncio in CircuitPython 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)

Moves CancelledError & creates InvalidStateError in the extmod

Adds a hash unary operation so Tasks can be added to sets.

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.
@imnotjames
Copy link
Author

This is a follow up from #8576 & is the same code as in micropython#13000

This is different from #8576 in that it doesn't define the exceptions as part of the C code and instead pulls them in from the asyncio context that's passed to the Task. While I personally think that approach is an anti-pattern (I think most of that could be avoided by defining the exceptions in C & fixing how the loop is defined) it does match up more uniformly with the current approach.

@imnotjames
Copy link
Author

Ouch. Build failed for one of the smallest devices that there's not enough flash. This increases things by a few hundred bytes so that might indeed be the case?

If that's the only failure I'll be opening this PR

@imnotjames imnotjames marked this pull request as ready for review November 22, 2023 05:54
@dhalbert
Copy link
Collaborator

This board should have CIRCUITPY_ULAB turned off. That will make plenty of room. Not sure why longints are turned off on both these boards. The maker chose the original settings. Given what it's for, I think ulab off is fine.

drops ulab from the thunderpack v12 to drastically reduce the byte usage of flash
@imnotjames
Copy link
Author

imnotjames commented Nov 22, 2023

Added the commit from #8650 to this PR instead per request - to reduce the number of full builds required.

@dhalbert
Copy link
Collaborator

I"m going to make this a draft while micropython#13000 is in process.

@dhalbert dhalbert marked this pull request as draft November 28, 2023 15:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants