Created
July 13, 2023 20:13
-
-
Save LyleScott/abef0ae38fec17c0162365cfbcd581ef to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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