Skip to content

Instantly share code, notes, and snippets.

@LyleScott
Created July 13, 2023 20:13
Show Gist options
  • Save LyleScott/abef0ae38fec17c0162365cfbcd581ef to your computer and use it in GitHub Desktop.
Save LyleScott/abef0ae38fec17c0162365cfbcd581ef to your computer and use it in GitHub Desktop.
import aiohttp
def request_tracer(results_collector):
async def on_request_start(session, context, params):
context.on_request_start = session.loop.time()
context.is_redirect = False
async def on_request_end(session, context, params):
total = session.loop.time() - context.on_request_start
context.on_request_end = total
results_collector['total'] = round(total * 1000, 2)
trace_config = aiohttp.TraceConfig()
trace_config.on_request_start.append(on_request_start)
trace_config.on_request_end.append(on_request_end)
return trace_config
import aiohttp
import asyncio
async def fetch(url):
trace = {}
async with aiohttp.ClientSession(trace_configs=[request_tracer(trace)]) as client:
async with client.get(url) as response:
print(trace)
urls = [
'https://github.com'
]
loop = asyncio.get_event_loop()
loop.run_until_complete(asyncio.gather(*[fetch(url) for url in urls]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment