Why does executing async function returns <coroutine> instead of a promise ?

75 views
Skip to first unread message

Denis Migdal

unread,
Sep 30, 2023, 3:55:54 PM9/30/23
to brython
Hi,


I was playing with async/await and aio in Brython.

I was wondering why executing async functions don't return a Promise instead of having to go to the trouble to use aio.run() to really execute the functions (which seems a quite weird).

Indeed, in Brython, using aio.run() goes against asyncio principles as aio.run() must be executed only once (while in Brython we need to execute it for each call to async function), moreover, Brython does not need an event loop as the browser already have one.

If we wanted to do as asyncio does, shouldn't aio.run() be replaced by aio.create_task() ?

Also, instead of creating a coroutine when an async function is called (which is strange as calling async JS function in Brython does return a Promise), why not executing it normally, and provide 2 ways of using aio.create_task() ?
- aio.create_task( <JS Promise> ) => returns Promise? (do nothing itself)
- aio.create_task( <coroutine> ) => returns Promise?
as well as a function : aio.create_coroutine(fct, arguments) which would enable to create coroutine from Brython but also JS async functions.

What do you think ?


Cordially,

Denis Migdal

unread,
Sep 30, 2023, 4:19:07 PM9/30/23
to brython
I think Brython should be coherent about async function, by either :
- A. JS async function call are modified in Brython so that they return a coroutine (which could be quite strange but more familiar to Python devs).
- B. Bry async functions behave like normal JS async function, and eventually a new keyword is introduced to create coroutine (e.g. bry_async) ?
- C. Bry async and JS async functions behave as usual, and a new keyword is introduced to create JS async function in Brython (e.g. js_async) ?

Still I think aio.run() should be depreciated and replaced by aio.create_task() :
- it'll be more coherent with asyncio usage
- it'll enable to get a Promise from the return value (that is currently to possible).

In solutions B&C, a aio.create_coroutine() may be useful if a brython dev wants to create a coroutine from a JS async function (?).
Reply all
Reply to author
Forward
0 new messages