Issue
I am trying to use asyncio with a method I have which takes a while to run. I want to call the same method many times with slightly different inputs and I don't care which call completes first, I just want to gather up all the results when they are all finished (prime candidate for asyncio loop right?) Here is some code I have been using when I know how many times I want to call my method:
async def main():
loop = asyncio.get_event_loop()
future1 = loop.run_in_executor(None, myhttpreq, 'US')
future2 = loop.run_in_executor(None, myhttpreq, 'RU')
future3 = loop.run_in_executor(None, myhttpreq, 'CN')
response1 = await future1
response2 = await future2
response3 = await future3
print(response1)
print(response2)
print(response3)
As you can tell by the params being passed with myhttpreq the reason this method takes so long is because it calls endpints from all over the world. My question is this:
Say the list of countries I want to call is pretty variable, how can I generate, and await on a variable amount of futures?
Solution
I ended up doing this:
async def main2(countries):
loop = asyncio.get_event_loop()
futures = []
for country in countries:
futures.append(loop.run_in_executor(None, myhttpreq, country))
for future in futures:
print(await future)
Does this seem dumb though?
Answered By - Rosey
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.