-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Remove 'Implicit Async', Don't Await runtime.run()
#928
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
Merged
JeffersGlass
merged 17 commits into
pyscript:main
from
JeffersGlass:dont-await-runtime-run
Nov 16, 2022
Merged
Changes from 1 commit
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
56b9bc5
Throw UserError if top-level await found in source
JeffersGlass 140d78b
Use runPython, un-async function call chain
JeffersGlass c28f86e
Adjust UserError message formatting
JeffersGlass 343c6b2
New async tests
JeffersGlass 60cf424
De-async runButDontRaise
JeffersGlass 0418a84
test_execute_on_shift_enter no longer needs to wait
JeffersGlass cb49970
Remove implicit coroutine scheduling from examples
JeffersGlass 2d3b4d2
Stub of location for docs
JeffersGlass 98465a4
Async coroutine docs
JeffersGlass 4f487e4
Add link to new docs to UserError
JeffersGlass d0577dc
get_event_loop -> get_running_loop
JeffersGlass 345666d
Revert to get_event_loop, adjust test
JeffersGlass 1edaf85
PyRepl.execute() is no long async, returns null
JeffersGlass db341ad
PyRepl.execute return type: null -> void
JeffersGlass ef62f95
runtime.run should not return promise
JeffersGlass d7db729
Add tests for uses_top_level_await
JeffersGlass b206943
xfail test_importmap
JeffersGlass 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
Async coroutine docs
- Loading branch information
commit 98465a4a3524ad62ab8cdba1af001e143f88ade7
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,35 @@ | ||
# Using Async/Await and Asyncio | ||
|
||
## {bdg-warning-line}`Deprecated` Implicit Coroutine Scheduling / Top-Level Await | ||
|
||
In PyScript versions 2022.09.1 and earlier, \<py-script\> tags could be written in a way that enabled "Implicit Coroutine Scheduling." The keywords `await`, `async for` and `await with` were permitted to be used outside of `async` functions. Any \<py-script\> tags with these keywords at the top level were compiled into coroutines and automatically scheuled to run in the browser's event loop. This functionality was deprecated, and these keywords are no longer allowed outside of `async` functions. | ||
|
||
To transition code from using top-level await statements to the currently-acceptable syntax, wrap the code into a coroutine using `async def()` and schedule it to run in the browser's event looping using `asyncio.ensure_future()` or `asyncio.create_task()`. | ||
|
||
The following two pieces of code are functionally equivalent - the first only works in versions 2022.09.1, the latter is the currently acceptable equivalent. | ||
|
||
```python | ||
# This version is deprecated, since | ||
# it uses 'await' outside an async function | ||
<py-script> | ||
import asyncio | ||
|
||
for i in range(3): | ||
print(i) | ||
await asyncio.sleep(1) | ||
</py-script> | ||
``` | ||
|
||
```python | ||
# This version is acceptable | ||
<py-script> | ||
import asyncio | ||
|
||
async def main(): | ||
for i in range(3): | ||
print(i) | ||
await asyncio.sleep(1) | ||
|
||
asyncio.ensure_future(main()) | ||
</py-script> | ||
``` |
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
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.
Uh oh!
There was an error while loading. Please reload this page.