Issue
I need to gather results from async calls and return them to an ordinary function--bridging the async and sync code sections confuses me.
First try, with an ugly inout param.
import asyncio
import aiohttp
async def one_call(url):
async with aiohttp.ClientSession() as session:
async with session.get(url) as response:
txt = await response.text()
return txt[0:20]
async def do_all(result_inout):
urls = ["https://cnn.com", "https://nyt.com", "http://reuters.com"]
out = await asyncio.gather(*[one_call(url) for url in urls])
result_inout += out
if __name__ == "__main__":
result_inout = []
asyncio.run(do_all(result_inout))
print(result_inout)
Second try, but with direct use of the event loop, which is discouraged for application code.
if __name__ == "__main__":
# same imports, same one_call, same urls
loop = asyncio.get_event_loop()
aggregate_future = asyncio.gather(*[one_call(url) for url in urls])
results = loop.run_until_complete(aggregate_future)
loop.close()
print(results)
What is the best way to do this?
Solution
Do not need result_inout
, you can just use out = asyncio.run(do_all())
to get the return res
of do_all
.
import asyncio
import aiohttp
async def one_call(url):
await asyncio.sleep(2)
return 0
async def do_all():
urls = ["https://cnn.com", "https://nyt.com", "http://reuters.com"]
out = await asyncio.gather(*[one_call(url) for url in urls])
return out
if __name__ == "__main__":
out = asyncio.run(do_all())
print(out)
out will be [0, 0, 0]
.
Answered By - LiuXiMin
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.