Skip to content

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
merged 17 commits into from
Nov 16, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Async coroutine docs
  • Loading branch information
JeffersGlass committed Nov 16, 2022
commit 98465a4a3524ad62ab8cdba1af001e143f88ade7
34 changes: 34 additions & 0 deletions docs/guides/asyncio.md
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>
```
2 changes: 2 additions & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ Check out our [getting started guide](tutorials/getting-started.md)!
You already know the basics and want to learn specifics!

[Passing Objects between JavaScript and Python](guides/passing-objects.md)

[Making async HTTP requests in pure Python](guides/http-requests.md)

[Async/Await and Asyncio](guides/asyncio.md)

:::
Expand Down
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy