Running WSGI Apps with aiohttp

Feb 22, 2024 ยท 2 min read

The aiohttp library in Python provides great support for asynchronous HTTP handling. However, many Python web applications are written using the WSGI standard interface. Thankfully, aiohttp provides a way to run WSGI apps directly, making it easy to leverage aiohttp's performance while still supporting existing WSGI apps.

Why Run WSGI in aiohttp?

There are a few key reasons you may want to run WSGI apps with aiohttp:

  • Performance - aiohttp is fast and scalable for asynchronous workloads. Running WSGI via aiohttp can provide better throughput and lower latency.
  • Existing apps - If you have an existing, complex WSGI app you want to make asynchronous, hosting it in aiohttp is easier than rewriting from scratch.
  • Ecosystem - aiohttp has a rich ecosystem of tools and extensions for tasks like monitoring, security, routing etc. Gaining access to these can benefit WSGI apps.
  • How the aiohttp WSGI Hosting Works

    Under the hood, aiohttp implements a WSGI handler using a threadpool. Each WSGI request is handled in a separate thread using a thread pool executor. This allows the WSGI app to perform blocking I/O without impacting overall server performance.

    Here is a simple example:

    from aiohttp import web
    import aiohttp_wsgi
    
    def wsgi_app(environ, start_response):
      #...
      start_response('200 OK', [('Content-Type', 'text/plain')]) 
      return [b'Hello World']
    
    app = web.Application()
    app.router.add_route("*", "/{path_info:.*}", aiohttp_wsgi.WSGIHandler(wsgi_app))
    web.run_app(app)

    This mounts the WSGI app at the path /{path_info:.*}, handling all requests with the app.

    Considerations for WSGI in aiohttp

    There are a few things to keep in mind:

  • WSGI apps cannot perform asynchronous I/O - they are still limited to blocking calls
  • There is a performance overhead from the threadpool and context switching
  • Long-running WSGI requests can starve the threadpool and impact performance
  • Overall though, for many workloads hosting WSGI in aiohttp provides an excellent performance boost. Care should be taken with expensive blocking I/O calls inside WSGI apps.

    Browse by tags:

    Browse by language:

    The easiest way to do Web Scraping

    Get HTML from any page with a simple API call. We handle proxy rotation, browser identities, automatic retries, CAPTCHAs, JavaScript rendering, etc automatically for you


    Try ProxiesAPI for free

    curl "http://api.proxiesapi.com/?key=API_KEY&url=https://example.com"

    <!doctype html>
    <html>
    <head>
        <title>Example Domain</title>
        <meta charset="utf-8" />
        <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1" />
    ...

    X

    Don't leave just yet!

    Enter your email below to claim your free API key: