your code in main
itself never runs - you don´t call the event loop:
while asyncio.create_task
will create a task and get it running "in the background" when you are already running asynchronous code, that never happens in this example:
Your modulelevel code gets a reference to an event loop, with get_event_loop
, but uses create_task(main())
- that will create a task to run the main function, but you then never switch control to the asyncio event loop itself.
A more proper way isto have your synchronous code define things up to the point everything is ready, and then pass the control to the event loop so that it executes a "parent" task that may or not spawn others.
In recent Python versions, this is simply done with asyncio.run()
- with no need to get an explicit reference to the loop prior to that. In older Python one would have to call get_event_loop()
as you do, and would usually call loop.run_until_complete(main())
.
There are other strange things in your code - including the use of os.system
just to clear the screen - just print the ANSI sequence "\x1b[2J" on Unixes, or
print("\n" * 70)` in any system.
The other thing is that you should use await asyncio.sleep(...)
when using asyncio code, and never time.sleep
import asyncio
...
async def main():
a = "a"
while True:
update(a)
a += "c"
await asyncio.sleep(0.05)
asyncio.run(main())