Skip to content

Instantly share code, notes, and snippets.

@thehappycheese
Created February 19, 2024 03:50
Show Gist options
  • Save thehappycheese/7254c35b517ba58f983157cf3b39cdfa to your computer and use it in GitHub Desktop.
Save thehappycheese/7254c35b517ba58f983157cf3b39cdfa to your computer and use it in GitHub Desktop.
pandas_aoihttp
import pandas as pd
import asyncio
from aiohttp import ClientSession, TCPConnector
@pd.api.extensions.register_series_accessor("aiohttp")
class AiohttpAccessor:
def __init__(self, pandas_obj):
self._obj = pandas_obj
async def _fetch_text(self, url: str, session):
async with session.get(url) as response:
return await response.text()
async def get(self, limit:int=100,ssl: bool=True):
async with ClientSession(connector=TCPConnector(limit=limit, ssl=ssl)) as session:
work = [self._fetch_text(url, session) for url in self._obj]
responses = await asyncio.gather(*work)
return pd.Series(responses)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment