Skip to content

Instantly share code, notes, and snippets.

@ivanzvonkov
Last active September 17, 2020 11:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ivanzvonkov/2ca88134747624f38c862ee0cbc83101 to your computer and use it in GitHub Desktop.
Save ivanzvonkov/2ca88134747624f38c862ee0cbc83101 to your computer and use it in GitHub Desktop.
import io
import asyncio
import aiohttp
import nest_asyncio
nest_asyncio.apply()
async def fetch_dataframe(session, url):
""" Asynchronously fetches a csv file using the url """
async with session.get(url) as response:
byte_code = await response.content.read()
df = pd.read_csv(io.BytesIO(byte_code), encoding='utf8')
return df
async def fetch_concurrent(urls):
""" Makes concurrent calls """
loop = asyncio.get_event_loop()
async with aiohttp.ClientSession() as session:
tasks = []
for url in urls:
tasks.append(loop.create_task(fetch_dataframe(session, url)))
all_dfs = []
# Loop through each completed task
for df in asyncio.as_completed(tasks):
df = await df
all_dfs.append(df)
# Concatenate all fetched dataframes into one
return pd.concat(all_dfs, ignore_index=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment