Skip to content Skip to sidebar Skip to footer

Aiohttp Slowness With Threading

I copied the code from How to run an aiohttp server in a thread?. It runs fine. So I am adding one second sleep. When I launch 10 requests at the same time. The average response ti

Solution 1:

Use asyncio.sleep instead. Your setup is running coros that hard sleep 1 second before they yield to the event loop. So if you gather a bunch of them you have to wait that 1 second for each one serially.

Solution 2:

You are starting the server in a second thread, but all of the requests are served from the same thread. The call to time.sleep blocks this thread and does not yield to the event loop so that the requests are effectively processed serially.

If you genuinely want to use sleep for a delay in the response you could use asyncio.sleep instead, which yields to the event loop.

However I expect you are using it as a placeholder for another blocking function. In this case you need to run this in another thread to the main server. The example below shows how to do this using run_in_executor and asyncio.wait.

import asyncio
from aiohttp import web
from concurrent.futures import ThreadPoolExecutor
import time


def blocking_func(seconds: int) -> int:
    time.sleep(seconds)
    return seconds


async def view_page(request: web.Request):
    seconds = int(request.query.get("seconds", 5))
    executor = request.app["executor"]
    loop = asyncio.get_event_loop()
    task = loop.run_in_executor(executor, blocking_func, seconds)
    completed, pending = await asyncio.wait([task])
    result = task.result()
    return web.Response(text=f"Waited {result} second(s).")


def create_app():
    app = web.Application()
    app.add_routes([web.get("/", view_page)])
    executor = ThreadPoolExecutor(max_workers=3)
    app["executor"] = executor
    return app


if __name__ == "__main__":
    app = create_app()
    web.run_app(app)

Post a Comment for "Aiohttp Slowness With Threading"