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.