-
Hello! Not sure if I'm using the right terms. I think I saw somewhere how to use asyncio to send multiple requests asynchronously, and get their results at the end, but I can't find it anymore. My goal is simply to avoid wasting time running requests concurrently in a for loop. I'd like to send them all at once, then wait them all and use their results. Could somebody help me 🙂 ? I think this could also be a great addition to the docs! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Ah, I think I can use UPDATE: OK I was able to write something 🙂 loop = asyncio.get_event_loop()
client = httpx.AsyncClient()
to_get = ["package1", "package2"]
coroutines = [client.get(f"https://pypi.python.org/pypi/{dep}/json") for dep in to_get]
responses = loop.run_until_complete(asyncio.gather(*coroutines))
loop.run_until_complete(client.aclose())
for response in responses:
pkg_data = response.json()["info"]
# use pkg_data... |
Beta Was this translation helpful? Give feedback.
Ah, I think I can use
asyncio.gather
for this: https://stackoverflow.com/questions/42231161/asyncio-gather-vs-asyncio-waitUPDATE: OK I was able to write something 🙂