-
-
Notifications
You must be signed in to change notification settings - Fork 7.4k
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
webassembly: Add native asyncio support #14328
Merged
dpgeorge
merged 7 commits into
micropython:master
from
dpgeorge:webassembly-add-asyncio-v2
Apr 25, 2024
Merged
webassembly: Add native asyncio support #14328
dpgeorge
merged 7 commits into
micropython:master
from
dpgeorge:webassembly-add-asyncio-v2
Apr 25, 2024
+479
−18
Conversation
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
dpgeorge
force-pushed
the
webassembly-add-asyncio-v2
branch
from
April 19, 2024 02:41
0c034a5
to
11177b4
Compare
Code size report:
|
dpgeorge
force-pushed
the
webassembly-add-asyncio-v2
branch
from
April 24, 2024 03:22
11177b4
to
6f2160c
Compare
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## master #14328 +/- ##
=======================================
Coverage 98.39% 98.39%
=======================================
Files 161 161
Lines 21204 21204
=======================================
Hits 20864 20864
Misses 340 340 ☔ View full report in Codecov by Sentry. |
dpgeorge
force-pushed
the
webassembly-add-asyncio-v2
branch
from
April 24, 2024 04:03
6f2160c
to
e5c1009
Compare
Signed-off-by: Damien George <damien@micropython.org>
An exception on the Python side should be passed to the Promise reject callback on the JavaScript side. Signed-off-by: Damien George <damien@micropython.org>
When a Promise is rejected on the JavaScript side, the reject reason should be thrown into the encapsulating generator on the Python side. Signed-off-by: Damien George <damien@micropython.org>
So it can be accessed by a port if needed, for example to see if asyncio has been imported. Signed-off-by: Damien George <damien@micropython.org>
This function exposes `setTimeout()` as an async function. Signed-off-by: Damien George <damien@micropython.org>
This commit adds a significant portion of the existing MicroPython asyncio module to the webassembly port, using parts of the existing asyncio code and some custom JavaScript parts. The key difference to the standard asyncio is that this version uses the JavaScript runtime to do the actual scheduling and waiting on events, eg Promise fulfillment, timeouts, fetching URLs. This implementation does not include asyncio.run(). Instead one just uses asyncio.create_task(..) to start tasks and then returns to the JavaScript. Then JavaScript will run the tasks. The implementation here tries to reuse as much existing asyncio code as possible, and gets all the semantics correct for things like cancellation and asyncio.wait_for. An alternative approach would reimplement Task, Event, etc using JavaScript Promise's. That approach is very difficult to get right when trying to implement cancellation (because it's not possible to cancel a JavaScript Promise). Signed-off-by: Damien George <damien@micropython.org>
This allows a simple way to run the existing asyncio tests under the webassembly port, which doesn't support `asyncio.run()`. Signed-off-by: Damien George <damien@micropython.org>
dpgeorge
force-pushed
the
webassembly-add-asyncio-v2
branch
from
April 24, 2024 06:31
e5c1009
to
49af8ca
Compare
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
This PR adds a significant portion of the existing MicroPython asyncio module to the webassembly port, using parts of the existing asyncio code and some custom JavaScript parts.
The key difference to the standard asyncio is that this version uses the JavaScript runtime to do the actual scheduling and waiting on events, eg Promise fulfillment, timeouts, fetching URLs.
This implementation does not include
asyncio.run()
. Instead one just usesasyncio.create_task(..)
to start tasks and then returns to the JavaScript. Then JavaScript will run the tasks.The implementation here tries to reuse as much existing asyncio code as possible, and gets all the semantics correct for things like cancellation and
asyncio.wait_for
. It's is different to #14193 which reimplemented tasks etc using JavaScript Promise's. That approach is very difficult to get right when trying to implement cancellation (because it's not possible to cancel a JavaScript Promise).