[Python-ideas] Top Level Await in Python like in Deno

54 views
Skip to first unread message

redr...@gmail.com

unread,
Feb 6, 2021, 5:27:31 AM2/6/21
to python...@python.org
Hi all,

Deno is JavaScript runtime that has very nice feature like Top Level Await, I think it would be also nice to have such feature in Python, it will make using async/await more convenient

What do you think ? Share your ideas lets discuss ...
_______________________________________________
Python-ideas mailing list -- python...@python.org
To unsubscribe send an email to python-id...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at https://mail.python.org/archives/list/python...@python.org/message/N2GA3CIY75TZNQ5RGKDMH7DDZNWLJNSH/
Code of Conduct: http://python.org/psf/codeofconduct/

Steven D'Aprano

unread,
Feb 6, 2021, 8:13:58 AM2/6/21
to python...@python.org
On Sat, Feb 06, 2021 at 10:26:23AM -0000, redr...@gmail.com wrote:

> Deno is JavaScript runtime that has very nice feature like Top Level
> Await, I think it would be also nice to have such feature in Python,
> it will make using async/await more convenient
>
> What do you think ? Share your ideas lets discuss ...

I don't know what to think because I don't know what "top level await"
means to you. Can you show an example of what it would look like, how
you would use it, and *why* you would use it?

Can you show a simple example of a task that would be better with top
level await, and why it is better?


--
Steve
_______________________________________________
Python-ideas mailing list -- python...@python.org
To unsubscribe send an email to python-id...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at https://mail.python.org/archives/list/python...@python.org/message/LPNTX3V2HWVCYPJOGMLD7PQRQUMUGMJM/

redr...@gmail.com

unread,
Feb 6, 2021, 9:41:03 AM2/6/21
to python...@python.org
I mean to be able to do something like this:
```python
import asyncio

await asyncio.sleep(1);
```
_______________________________________________
Python-ideas mailing list -- python...@python.org
To unsubscribe send an email to python-id...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at https://mail.python.org/archives/list/python...@python.org/message/ED75ZXM5WSLZMIW6SXGLWBUIFJRINNQ2/

Guido van Rossum

unread,
Feb 6, 2021, 10:45:39 AM2/6/21
to redr...@gmail.com, python...@python.org
The proposal is to add a default event loop that is always active.

On Sat, Feb 6, 2021 at 06:41 <redr...@gmail.com> wrote:
I mean to be able to do something like this:
```python
import asyncio

await asyncio.sleep(1);
```


--
--Guido (mobile)

Matthias Bussonnier

unread,
Feb 6, 2021, 1:02:58 PM2/6/21
to Guido van Rossum, redr...@gmail.com, python-ideas
If it's for the REPL, it's already there, you simply need to start the
async REPL.

$ python -m asyncio
asyncio REPL 3.8.5 | packaged by conda-forge | (default, Sep 16 2020, 17:43:11)
[Clang 10.0.1 ] on darwin
Use "await" directly instead of "asyncio.run()".
Type "help", "copyright", "credits" or "license" for more information.
>>> import asyncio
>>> await asyncio.sleep(1)
>>>

compile and exec have a mode to allow top-level async as well if you
want it programmatically, then you can `await` your code object.
--
Ma
> _______________________________________________
> Python-ideas mailing list -- python...@python.org
> To unsubscribe send an email to python-id...@python.org
> https://mail.python.org/mailman3/lists/python-ideas.python.org/
> Message archived at https://mail.python.org/archives/list/python...@python.org/message/ATXEJR7GYY2E3B6WGC7Y2G7NL6WWZ33L/
_______________________________________________
Python-ideas mailing list -- python...@python.org
To unsubscribe send an email to python-id...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at https://mail.python.org/archives/list/python...@python.org/message/HWO5WJJ5HAOQC7DBLOBRE3VBRXPZL2GG/

redr...@gmail.com

unread,
Mar 24, 2021, 5:43:04 AM3/24/21
to python...@python.org
It is not the same, it will work in interactive mode .... But I want to run application without interactive mode
_______________________________________________
Python-ideas mailing list -- python...@python.org
To unsubscribe send an email to python-id...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at https://mail.python.org/archives/list/python...@python.org/message/O4ME7K7ZVXULFBLAVTP3CTKLXI6O4R65/

Joao S. O. Bueno

unread,
Mar 24, 2021, 10:13:42 AM3/24/21
to redr...@gmail.com, Python-Ideas

Joao S. O. Bueno

unread,
Mar 24, 2021, 10:15:55 AM3/24/21
to redr...@gmail.com, Python-Ideas
Sorry - previous reply was sent empty.

So, you probably can do with `asyncio.run`:
```
In [26]: import asyncio

In [27]: asyncio.run(asyncio.sleep(2))
```
https://docs.python.org/3/library/asyncio-task.html#asyncio.run

On Wed, 24 Mar 2021 at 06:44, <redr...@gmail.com> wrote:

redr...@gmail.com

unread,
Apr 15, 2021, 3:43:43 PM4/15/21
to python...@python.org
Yes I can, but I am taking about to use it without `asyncio.run`

Whenever Python in Top-Level faces with await it will wrap calling all top level statement in async function (for example)
_______________________________________________
Python-ideas mailing list -- python...@python.org
To unsubscribe send an email to python-id...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at https://mail.python.org/archives/list/python...@python.org/message/OV5EU3LLWRK5IDZS7V6YZUJI2HYM36FU/

AlexDeLorenzo.dev

unread,
May 6, 2021, 6:12:05 PM5/6/21
to python-ideas
I like this idea. It's already the default behavior of the IPython shell, and it would allow for easy scripting with async libraries. I think importing the asyncio module at the top level should do what Guido said, by adding a default event loop that's always active.
Reply all
Reply to author
Forward
0 new messages