async and await

115 views
Skip to first unread message

Pierre Quentel

unread,
Jul 3, 2018, 5:30:53 AM7/3/18
to brython
I have been trying to implement async and await, building on the "yield from" support and the asyncio implementation by Jonathan.

In the current development version, they don't raise syntax errors, "async def" creates a coroutine function, that when called returns a coroutine object - this was needed to support the typing and dataclasses modules, that I have added lately. But I wonder if we can make them work like in CPython - in fact, I think we can't.

Take this simple example:

import asyncio

async
def main(n):
    await asyncio
.sleep(n)
   
print("waking up")

loop
= asyncio.get_event_loop()
print("start")
loop
.run_until_complete(main(2))
print("done")

In CPython, loop.run_until_complete() is blocking ; the program prints "done" after the task main(2) has been completed, ie after a 2 second pause. With the current asyncio implementation in Brython, "done" is printed just after "start" and "waking up" is printed 2 seconds later.

I am afraid that we are in the same situation as for the input() built-in function mentioned by ProfGra a few days ago, because Javascript has no concept of blocking execution until a function is completed.

With generators, the source code can be modified because there is a keyword (yield) that tells us when the function must be paused, and we can create a function that will be called for the next iteration. In the example above, we can't rely on the function name (run_until_complete) to create a callback function with the rest of the code to be executed (the last line `print("done")`).

Do you think there is a solution ? If not, we should probably keep the support of these keywords and of asyncio, but discourage their use with appropriate messages, and advice to use the DOM-style event binding instead.


André

unread,
Jul 3, 2018, 5:58:56 AM7/3/18
to brython


On Tuesday, 3 July 2018 06:30:53 UTC-3, Pierre Quentel wrote:
I have been trying to implement async and await, building on the "yield from" support and the asyncio implementation by Jonathan.

In the current development version, they don't raise syntax errors, "async def" creates a coroutine function, that when called returns a coroutine object - this was needed to support the typing and dataclasses modules, that I have added lately. But I wonder if we can make them work like in CPython - in fact, I think we can't.

Take this simple example:

import asyncio

async
def main(n):
    await asyncio
.sleep(n)
   
print("waking up")

loop
= asyncio.get_event_loop()
print("start")
loop
.run_until_complete(main(2))
print("done")

In CPython, loop.run_until_complete() is blocking ; the program prints "done" after the task main(2) has been completed, ie after a 2 second pause. With the current asyncio implementation in Brython, "done" is printed just after "start" and "waking up" is printed 2 seconds later.

I am afraid that we are in the same situation as for the input() built-in function mentioned by ProfGra a few days ago, because Javascript has no concept of blocking execution until a function is completed.

With generators, the source code can be modified because there is a keyword (yield) that tells us when the function must be paused, and we can create a function that will be called for the next iteration. In the example above, we can't rely on the function name (run_until_complete) to create a callback function with the rest of the code to be executed (the last line `print("done")`).

Do you think there is a solution ?

What about https://stackoverflow.com/a/33292942/558799?   I must admit having no experience with using the newest features of javascript, including promises and async...

André

Pierre Quentel

unread,
Jul 5, 2018, 4:47:45 PM7/5/18
to brython

What about https://stackoverflow.com/a/33292942/558799?   I must admit having no experience with using the newest features of javascript, including promises and async...

André

I don't know if this could solve the problem, but async and await in Javascript are ES7 features, so it's not an option for Brython before some time.

S. H.

unread,
Jul 17, 2018, 3:05:54 AM7/17/18
to brython
Am Dienstag, 3. Juli 2018 11:30:53 UTC+2 schrieb Pierre Quentel:
Do you think there is a solution ? If not, we should probably keep the support of these keywords and of asyncio, but discourage their use with appropriate messages, and advice to use the DOM-style event binding instead.

I like the async/await keywords because they transport the logic of a function more clear then callbacks.
Even if they are not working the same way than in CPython, I would like to see them in Brython.

Maybe something like this:

import asyncio

async
def main(n):
    await asyncio
.sleep(n)
   
print("waking up")

asyncio.ensure_future(main(2))

and "get_event_loop" raises an NotImplementedError? To make it clear, that it is not supported by Brython.

Reply all
Reply to author
Forward
0 new messages