hi, Im a beginner in asyncio. I was trying out some small scripts to bet
the basic idea of asyncio. The below script is meant to print "hello"
after some time period "after" is expired. But i have a strange output
when i dont use asyncio.wait() inside run_until_complete().
code 1 : async def say_hello(after):
await asyncio.sleep(after)
print('hello {0}'.format(after),end='--\n')
if __name__ == '__main__':
loop = asyncio.get_event_loop()
a = count()
coros = []
tasks = []
for i in range(10):
#coros.append(say_hello(i))
tasks.append(asyncio.ensure_
future(say_hello(i)))
tasks = asyncio.wait(tasks)
loop.run_until_complete(tasks)
output
hello 0--
hello 1--
hello 2--
hello 3--
hello 4--
hello 5--
hello 6--
hello 7--
hello 8--
hello 9--
this works as intended.
code 2:
async def say_hello(after):
await asyncio.sleep(after)
print('hello {0}'.format(after),end='--\n')
if __name__ == '__main__':
loop = asyncio.get_event_loop()
a = count()
coros = []
tasks = []
for i in range(10):
#coros.append(say_hello(i))
tasks.append(asyncio.ensure_future(say_hello(i)))
#tasks = asyncio.wait(tasks)
loop.run_until_complete(tasks[6])
output
hello 0--
hello 1--
hello 2--
hello 3--
hello 4--
hello 5--
hello 6--
why did it give such an output?