aiohttp-fast-zlib

0.3.0 · active · verified Thu Apr 16

aiohttp-fast-zlib is a Python library (current version 0.3.0) that replaces the default `zlib` usage in `aiohttp` with faster alternatives like `isal` or `zlib-ng`. This significantly improves compression and decompression performance, especially for `aiohttp`'s WebSocket connections, where `zlib` can be a bottleneck. The library has seen several updates over the past year, indicating active maintenance and development.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to enable `aiohttp-fast-zlib` and then use a standard `aiohttp.ClientSession` to make a request. The `enable()` call patches `aiohttp` globally to use the faster zlib backend. You can set the `TEST_URL` environment variable to point to a service that returns gzipped content (e.g., `httpbin.org/gzip`) to observe its effect.

import aiohttp
import asyncio
import os
import aiohttp_zlib_fast

# Enable the fast zlib backend. isal is preferred if available, then zlib-ng, else standard zlib.
aiohttp_zlib_fast.enable()

async def fetch(session, url):
    async with session.get(url) as response:
        print(f"Status: {response.status}")
        print(f"Content-type: {response.headers.get('content-type')}")
        # If the response is compressed, aiohttp will decompress it using the enabled backend
        text = await response.text()
        print(f"Body snippet: {text[:100]}...")

async def main():
    async with aiohttp.ClientSession() as session:
        # Use a real URL, or an httpbin-like service that supports compression
        await fetch(session, os.environ.get('TEST_URL', 'http://httpbin.org/gzip'))

if __name__ == '__main__':
    asyncio.run(main())

view raw JSON →