This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Add support for top-level await
Type: enhancement Stage: resolved
Components: Interpreter Core Versions: Python 3.11
process
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: Nosy List: Fred, asvetlov, eric.araujo, gstarck, terry.reedy, yselivanov
Priority: normal Keywords:

Created on 2021-10-27 11:38 by Fred, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (12)
msg405088 - (view) Author: Fred (Fred) Date: 2021-10-27 11:38
I want top-level await without any boilerplate code or setup.
Just write a "await" statement on line 1 without any indention.

    #!/usr/bin/env python3
    import asyncio
    await asyncio.sleep(1)


I don't want to have to call asyncio.run(main()), and I don't  want to have to put the await inside an async function.
msg405382 - (view) Author: Éric Araujo (eric.araujo) * (Python committer) Date: 2021-10-30 18:00
Do you know about using "python -m asyncio" instead of "python"?
msg405387 - (view) Author: Fred (Fred) Date: 2021-10-30 21:47
No, I was not aware of that.

After I started Python by running "python3 -m asyncio" then it behaved as I expected it should behave by default.

But this seems to work only in the REPL. I would like to write Python scripts stored in files where I can easily await at the top level as in the example in my first post.
msg405411 - (view) Author: Éric Araujo (eric.araujo) * (Python committer) Date: 2021-10-31 17:36
I don’t think that’s possible; it wouldn’t fit with the design of python to have an implicit async loop like that.  But I’m no expert here, so you could bring this idea to https://discuss.python.org/c/ideas/6 to see what people think.
msg405430 - (view) Author: Fred (Fred) Date: 2021-11-01 10:42
I think it would fit with the design and commmunity of Python. I think people who use Python don't care about what async runtime it is, or how to configure it or set it up, they just want to call async functions.

This is 2021, we live in a async-first world. I think users expect to be able to do async with minimal friction.

Other languages like C# lets you do it easy, by changing:
    static void Main() {}
to:
    static async Task Main() {}
msg405442 - (view) Author: Éric Araujo (eric.araujo) * (Python committer) Date: 2021-11-01 14:14
But Python doesn’t have a concept of main function.

Anyway, it’s no use arguing here, please go to python-ideas for a wider audience.
msg405445 - (view) Author: Grégory Starck (gstarck) * Date: 2021-11-01 15:43
python --async myscript.py ?
msg405453 - (view) Author: Fred (Fred) Date: 2021-11-01 17:04
$ python3 --async myscript.py
unknown option --async

Also I cannot go around telling other people how to run my script. My script must be able to run properly without any special setup.
msg405484 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2021-11-02 03:55
Python has async frameworks other than anyncio.  At least one is much older than asyncio.  I think that this proposal to run one by default, on startup, should be rejected.

>My script must be able to run properly without any special setup.

Then write it with proper python, including the needed import of the async runner you want.
msg405498 - (view) Author: Fred (Fred) Date: 2021-11-02 09:55
I don't care what async framework is used, nor do I care if its a x86 or ARM, or if its Windows or Linux.

I don't want to have to setup an async runner because it is boilerplate code, and it brings concern into my application which is outside of the domain of my application.

In JavaScript, I can just call await fetch() or any other asynchronous function without having to pick and configure a async runtime.

On .NET it is also very easy, just await a function at the top-level without declare any runtime either. 
https://docs.microsoft.com/en-us/dotnet/csharp/whats-new/tutorials/top-level-statements

Python is suppose to be a high-level language. Programmers should focus on their domain, not underlying things such as which runtime to use, which garbage collector to use, etc.
msg405500 - (view) Author: Andrew Svetlov (asvetlov) * (Python committer) Date: 2021-11-02 10:44
As people write above, Python supports multiple async frameworks, asyncio is not the single choice.

You should select and maybe configure the async framework before executing your first `await` statement.

That's why the proposal doesn't work in Python.

Both javascript and C# are designed slightly differently: they have the only async subsystem that is deeply built into the language. It allows *implicit* async engine initialization and starting.

Sorry, should close the issue.
msg405501 - (view) Author: Fred (Fred) Date: 2021-11-02 10:47
Yeah, those other languages such as C# and JavaScript and others make async much more easier, approachable, reduces boilerplate code and lowers the bar for entry!
History
Date User Action Args
2022-04-11 14:59:51adminsetgithub: 89788
2021-11-02 10:47:09Fredsetmessages: + msg405501
2021-11-02 10:44:42asvetlovsetstatus: open -> closed
resolution: rejected
messages: + msg405500

stage: resolved
2021-11-02 09:55:43Fredsetmessages: + msg405498
2021-11-02 03:55:05terry.reedysetnosy: + terry.reedy, asvetlov, yselivanov
messages: + msg405484
2021-11-01 17:04:41Fredsetmessages: + msg405453
2021-11-01 15:43:10gstarcksetnosy: + gstarck
messages: + msg405445
2021-11-01 14:14:12eric.araujosetmessages: + msg405442
2021-11-01 10:42:40Fredsetmessages: + msg405430
2021-10-31 17:36:10eric.araujosetmessages: + msg405411
2021-10-30 21:47:34Fredsetmessages: + msg405387
2021-10-30 18:00:00eric.araujosetnosy: + eric.araujo
messages: + msg405382
2021-10-27 11:38:05Fredcreate