Issue
My code doesn't output any text, and then terminates after 5 seconds. Doing so in a non-async method does show output, but blocks other processes from running. Any thoughts and/or suggestions?
import asyncio, os, sys, time
def update(text):
# Area of interest
global print
print("", flush=True)
os.system("clear")
print(text, flush=True)
print("What is your selection? ", flush=True)
async def main():
a = "a"
while True:
update(a)
a += "c"
time.sleep(0.05)
loop = asyncio.get_event_loop()
loop.create_task(main())
time.sleep(5)
Solution
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())
Answered By - jsbueno
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.