I sure would love to post my experiments, but I'm really struggling to put it all together. I don't even know where to start. What I want seems fairly simple (just by looking at the sync version), and I'm new to python so I'm really in pain to make all of this work. In the end I just want a async getResults() function that can be called from elsewhere just as if it were a sync getResults().
From what I understand of your answers, I have no choice but to use the aiohttp. So the other pieces of code are irrelevant.
In the last example you're giving, it just prints the result, whereas I want to return it as a dictionary url -> response, and it doesn't handle a loop.
Here is what I've tried, but I don't know how wrong I am :
From what I understand of your answers, I have no choice but to use the aiohttp. So the other pieces of code are irrelevant.
In the last example you're giving, it just prints the result, whereas I want to return it as a dictionary url -> response, and it doesn't handle a loop.
Here is what I've tried, but I don't know how wrong I am :
async def getResultsAsync(urls): async def getResultAsync(url): async with aiohttp.ClientSession() as session: resp = await session.get(url) res = url, await resp.json() return res return dict(await asyncio.gather(*[getResultAsync(url) for url in urls])) def getResults(urls): return asyncio.run(getResultsAsync(url))