I’m trying to make a number of requests at the same time.
I’m new to async
and await
in Python (I’ve used it in Js).
I found an example and used this:
import asyncio
import aiohttp
async def get_item( session: aiohttp.ClientSession, path: str ):
# fetch data
url = f'my/path/here{path}'
resp = await session.request('GET', url=url)
data = await resp.json()
# at this point I do stuff to the data and save as csv
return await data
async def get_all():
tasks = []
async with aiohttp.ClientSession() as session:
tasks.append(get_item(session=session, path='1'))
tasks.append(get_item(session=session, path='2'))
return await asyncio.gather(*tasks, return_exceptions=True)
loop = asyncio.get_event_loop()
loop.run_until_complete(get_all())
but I get the following error:
RuntimeError: This event loop is already running
Obviously there is an issue, please can someone explain what the issue is and how I can fix this?