Skip to content

Instantly share code, notes, and snippets.

@imbolc
Created October 27, 2015 16:59
Show Gist options
  • Save imbolc/e84728340dfb341c30ae to your computer and use it in GitHub Desktop.
Save imbolc/e84728340dfb341c30ae to your computer and use it in GitHub Desktop.
Understanding aiohttp.ClientSession
from time import time
import asyncio
import aiohttp
from aiohttp import web
async def web_handler(request):
n = int(request.GET.get('n', 0))
return web.Response(text=str(n+1))
async def init_app(loop):
app = web.Application(loop=loop)
app.router.add_route('GET', '/', web_handler)
return app
class TaskPool:
def __init__(self, tasks, max_threads=10, loop=None):
self.semaphore = asyncio.Semaphore(max_threads)
self.loop = loop or asyncio.get_event_loop()
self.coros = [self.add_task(task) for task in tasks]
def run(self):
self.loop.run_until_complete(asyncio.wait(self.coros))
async def add_task(self, task):
with (await self.semaphore):
await self.process_task(task)
async def process_task(self, task):
raise NotImplementedError
class Client(TaskPool):
session = aiohttp
async def process_task(self, n):
async with self.session.get('http://127.0.0.1:8000',
params={'n': n}) as r:
result = int(await r.text())
assert result == n + 1
class SessionClient(Client):
session = aiohttp.ClientSession()
def bench(client_cls, num_requests, num_threads):
client = client_cls(range(num_requests), num_threads)
started = time()
client.run()
took = time() - started
print('{:15} made {} requests with {:2} threads in {} seconds'.format(
client_cls.__name__, num_requests, num_threads, took))
if __name__ == '__main__':
loop = asyncio.get_event_loop()
app = loop.run_until_complete(init_app(loop))
handler = app.make_handler()
srv = loop.run_until_complete(
loop.create_server(handler, '127.0.0.1', 8000))
print('Serving at http://{}:{}/'.format(*srv.sockets[0].getsockname()))
bench(Client, 100, 1)
bench(SessionClient, 100, 1)
bench(Client, 100, 10)
bench(SessionClient, 100, 10)
SessionClient.session.close()
loop.run_until_complete(handler.finish_connections(1.0))
srv.close()
loop.run_until_complete(srv.wait_closed())
loop.run_until_complete(app.finish())
loop.close()
Client made 100 requests with 1 threads in 0.3546574115753174 seconds
SessionClient made 100 requests with 1 threads in 3.984743595123291 seconds
Client made 100 requests with 10 threads in 0.32209348678588867 seconds
SessionClient made 100 requests with 10 threads in 0.42948365211486816 seconds
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment