Issue
Function cant pass to next line because of asyncio.sleep. There is rest of the code but i will share just 3 lines. It explains everything. Console doesnt print 0 to console. If i move print(0) above of asyncio.sleep it prints to console.
async def getHistory(self):
logging.info(f"Getting history for {self.parite}...")
await asyncio.sleep(1)
print(0)
async def get_histories():
for parite in parite_list:
asyncio.create_task(parite.getHistory())
asyncio.run(get_histories())
Solution
It looks like you created the tasks but did not execute them. Try this with asyncio.gather:
import asyncio
import logging
async def getHistory(num):
print(f"Getting history for {num}...")
await asyncio.sleep(1)
print(num)
async def get_histories():
await asyncio.gather(
asyncio.create_task(getHistory(1)),
asyncio.create_task(getHistory(2)),
asyncio.create_task(getHistory(3))
)
asyncio.run(get_histories())
Result:
% python asdf.py
Getting history for 1...
Getting history for 2...
Getting history for 3...
1
2
3
Answered By - Matthew Trotter
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.