{"id":46430758,"url":"https://github.com/tarasko/aiofastnet","last_synced_at":"2026-05-28T01:06:19.633Z","repository":{"id":341916809,"uuid":"1171614105","full_name":"tarasko/aiofastnet","owner":"tarasko","description":"Ultra-fast networking with Kernel TLS support for asyncio and Python ","archived":false,"fork":false,"pushed_at":"2026-05-27T19:19:49.000Z","size":738,"stargazers_count":17,"open_issues_count":2,"forks_count":5,"subscribers_count":0,"default_branch":"master","last_synced_at":"2026-05-27T19:23:49.015Z","etag":null,"topics":["async-await","asyncio","event-loop","high-performance","kernel-tls","networking","performance","protocol","python","python-3","python-library","python-networking","python3","ssl","ssl-tls","tls","transport"],"latest_commit_sha":null,"homepage":"","language":"Cython","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/tarasko.png","metadata":{"files":{"readme":"README.md","changelog":"HISTORY.rst","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":"SECURITY.md","support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":"AGENTS.md","dco":null,"cla":null}},"created_at":"2026-03-03T12:26:03.000Z","updated_at":"2026-05-25T19:35:35.000Z","dependencies_parsed_at":null,"dependency_job_id":null,"html_url":"https://github.com/tarasko/aiofastnet","commit_stats":null,"previous_names":["tarasko/aiofastnet"],"tags_count":15,"template":false,"template_full_name":null,"purl":"pkg:github/tarasko/aiofastnet","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tarasko%2Faiofastnet","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tarasko%2Faiofastnet/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tarasko%2Faiofastnet/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tarasko%2Faiofastnet/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/tarasko","download_url":"https://codeload.github.com/tarasko/aiofastnet/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tarasko%2Faiofastnet/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":33589689,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-26T15:22:16.424Z","status":"online","status_checked_at":"2026-05-27T02:00:06.184Z","response_time":53,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["async-await","asyncio","event-loop","high-performance","kernel-tls","networking","performance","protocol","python","python-3","python-library","python-networking","python3","ssl","ssl-tls","tls","transport"],"created_at":"2026-03-05T18:08:52.965Z","updated_at":"2026-05-28T01:06:19.627Z","avatar_url":"https://github.com/tarasko.png","language":"Cython","funding_links":[],"categories":[],"sub_categories":[],"readme":"# aiofastnet\n\n[![Test status](https://img.shields.io/github/actions/workflow/status/tarasko/aiofastnet/run-tests.yml?branch=master\u0026label=tests)](https://github.com/tarasko/aiofastnet/actions/workflows/run-tests.yml?query=branch%3Amaster)\n[![Latest PyPI package version](https://badge.fury.io/py/aiofastnet.svg)](https://pypi.org/project/aiofastnet)\n[![Downloads count](https://img.shields.io/pypi/dm/aiofastnet.svg)](https://pypistats.org/packages/aiofastnet)\n\n`aiofastnet` provides drop-in, highly efficient C/Cython replacements for asyncio's:\n\n- [`loop.create_connection()`](https://docs.python.org/3/library/asyncio-eventloop.html#asyncio.loop.create_connection)\n- [`loop.open_connection()`](https://docs.python.org/3/library/asyncio-stream.html#asyncio.open_connection)\n- [`loop.create_server()`](https://docs.python.org/3/library/asyncio-eventloop.html#asyncio.loop.create_server)\n- [`loop.start_server()`](https://docs.python.org/3/library/asyncio-stream.html#asyncio.start_server)\n- [`loop.start_tls()`](https://docs.python.org/3/library/asyncio-eventloop.html#asyncio.loop.start_tls)\n- [`loop.sendfile()`](https://docs.python.org/3/library/asyncio-eventloop.html#asyncio.loop.sendfile)\n\nIf your library or application already uses the `asyncio` streams or transport/protocol\nmodel, `aiofastnet` lets you keep the same architecture while replacing one of\nthe most expensive layers underneath it.\n\n`aiofastnet` is not a different event loop. It works on top of stock `asyncio`\nloops or `uvloop` by using low-level primitives such as `add_reader` and\n`add_writer`. It has no background threads and does not use unscalable tricks such as\ncalling synchronous `recv`/`send` syscalls from another thread. Essentially, it\nprovides the same kind of internal implementation you would find in `asyncio`\nand `uvloop`, but implemented much more efficiently.\n\n`aiofastnet` supports [Kernel TLS](https://www.kernel.org/doc/html/latest/networking/tls.html) out of the box on Linux.\n\n## Benchmark\n\nThe benchmark below compares echo round-trips over loopback for TCP and SSL.\nThe exact gains depend on workload, message sizes, CPU, OpenSSL version, and how\nmuch of your total runtime is spent in transport/SSL plumbing.\n\n[![Benchmark](https://raw.githubusercontent.com/tarasko/aiofastnet/master/examples/benchmark.png)](https://github.com/tarasko/aiofastnet/blob/master/examples/benchmark.png)\n\nSource: [examples/benchmark.py](https://github.com/tarasko/aiofastnet/blob/master/examples/benchmark.py)\n\nIn these benchmarks, `aiofastnet` is up to 2.2x faster than standard\n`asyncio` and up to 1.6x faster than uvloop for TLS connections.\n\n`aiofastnet` is fully compatible with free-threaded Python builds and scales\nas expected when multiple event loops run in parallel across multiple threads.\n\n[![Threaded benchmark](https://raw.githubusercontent.com/tarasko/aiofastnet/master/examples/benchmark_threaded.png)](https://github.com/tarasko/aiofastnet/blob/master/examples/benchmark_threaded.png)\n\nSource: [examples/benchmark_threaded.py](https://github.com/tarasko/aiofastnet/blob/master/examples/benchmark_threaded.py)\n\n## Why Use aiofastnet\n\n- **Faster hot path**. Transport and SSL internals are reimplemented in Cython/C\n  to reduce overhead on long-lived connections.\n- **Drop-in API**. Keep using the standard `asyncio` streams or transport/protocol model\n  and familiar loop-level networking operations.\n- **Works with the event loop you already use**. `aiofastnet` works with\n  stock `asyncio` loops, `uvloop`, and `winloop`.\n- **Particularly strong for SSL-heavy workloads**. `aiofastnet` uses OpenSSL\n  more directly and avoids extra copies in the data path.\n- **Kernel TLS support on Linux**. Native `sendfile` for TLS connections through `SSL_sendfile`. \n- **Safer transport write() / writelines() behavior**. If the socket cannot accept\n  everything immediately, only `bytes` and `memoryview` objects backed by\n  `bytes` are retained without copying. Other objects, including\n  `bytearray` and non-`bytes` exporters, are copied before being queued.\n  Unlike standard `asyncio` transports, this avoids sending corrupted data if\n  application code modifies the underlying buffer after `write()` /\n  `writelines()` returns.\n- **Better SSL backpressure semantics**. Buffer sizes and write limits reflect\n  what is actually queued across the stack.\n- **Works for library authors**. WebSocket, HTTP, RPC, proxy, database, broker,\n  and custom protocol libraries can expose the same API while giving users a\n  faster transport layer.\n\n## Quickstart\n\nInstall from PyPI:\n\n```console\n$ pip install aiofastnet\n```\n\n`aiofastnet` requires Python 3.9 or greater.\n\nFor applications, the easiest way to enable `aiofastnet` is to use an event\nloop factory. This patches the loop's `create_connection()`, `create_server()`,\n`start_tls()`, and `sendfile()` methods while keeping the rest of the loop\nunchanged:\n\n```python\nimport asyncio\nimport aiofastnet\n\nasync def main():\n    ...\n\nasyncio.run(main(), loop_factory=aiofastnet.loop_factory())\n```\n\nIf you use another event loop implementation, pass its loop factory:\n\n```python\nimport asyncio\nimport uvloop\nimport aiofastnet\n\nasyncio.run(main(), loop_factory=aiofastnet.loop_factory(uvloop.new_event_loop))\n```\n\nFor older applications that still configure asyncio through event loop\npolicies, install the aiofastnet policy wrapper before creating loops:\n\n```python\nimport asyncio\nimport aiofastnet\n\naiofastnet.install_policy()\nasyncio.run(main())\n```\n\nEvent loop policies are deprecated in Python 3.14 and are scheduled for removal\nin Python 3.16, so prefer `loop_factory()` for new code.\n\nIf the event loop already exists, patch it directly:\n\n```python\nimport asyncio\nimport aiofastnet\n\nasync def main():\n    aiofastnet.patch_loop()\n    ...\n\nasyncio.run(main())\n```\n\nLibrary authors should call `aiofastnet` APIs directly\ninstead of patching a loop owned by their users. The API mirrors stdlib\n`asyncio`: pass the running loop and use your existing protocol factory.\n\n\n\n```python\nimport asyncio\nimport aiofastnet\n\nclass EchoClientProtocol(asyncio.Protocol):\n    def connection_made(self, transport):\n        self.transport = transport\n        self.transport.write(b\"hello\")\n\n    def data_received(self, data):\n        print(\"received:\", data)\n        self.transport.close()\n\nasync def main():\n    loop = asyncio.get_running_loop()\n\n    transport, protocol = await aiofastnet.create_connection(\n        loop,\n        EchoClientProtocol,\n        \"127.0.0.1\",\n        9000,\n    )\n\n    ...\n\nasyncio.run(main())\n```\n\nFor servers, replace `loop.create_server(...)` with\n`aiofastnet.create_server(loop, ...)` in the same way.\n\n`aiofastnet` also exposes `start_tls` and `sendfile`:\n\n```python\ntransport = await aiofastnet.start_tls(\n    loop,\n    transport,\n    protocol,\n    ssl_context,\n    server_side=False,\n    server_hostname=\"example.com\",\n)\n\nawait aiofastnet.sendfile(loop, transport, fileobj)\n```\n\n## When aiofastnet Is a Good Fit\n\n`aiofastnet` is most attractive when you already rely on `asyncio`'s\ntransport/protocol APIs and one or more of these are true:\n\n- You have long-lived TCP or TLS connections.\n- You run protocol-heavy services where transport overhead is visible in CPU\n  profiles.\n- You maintain a library and want better performance without changing your\n  public API.\n- You care about consistent `write()` / `writelines()` buffer behavior.\n- You want SSL flow control to reflect the whole buffered stack, not only part\n  of it.\n\n## When Not To Use aiofastnet\n\n`aiofastnet` is not the right default for every networking project:\n\n- If your workload is dominated by very short-lived connections, you should\n  expect little or no gain. `aiofastnet` focuses on optimizing the data path\n  after connection establishment.\n\n## Platform Compatibility\n\n`aiofastnet` is built and tested on Linux, macOS, and Windows. It works with\nthe standard `asyncio` event loop, `uvloop`, and `winloop`.\n\nOn Windows it works with `SelectorEventLoop` and `winloop`.\nWith `ProactorEventLoop` it falls back to `asyncio`'s native connection and\nserver implementation, because the proactor loop does not provide the\n`add_reader()` / `add_writer()` hooks required by `aiofastnet`'s custom\ntransport implementation. For transports created through `aiofastnet`, a\ncompatibility wrapper preserves the documented `write()` /\n`writelines()` buffer-safety behavior.\n\n## Kernel TLS Support on Linux\n\nOn Linux, `aiofastnet` can use OpenSSL's Kernel TLS support for TLS\nconnections. Kernel TLS is beneficial if any of the following is true:\n\n* Static files need to be sent over TLS connection and `sendfile()` can be used.\nIn that case the kernel can read data directly instead of forcing the application to \ncopy file contents through userspace.\n* Some high-end NICs support hardware TLS offload. This leads to huge CPU savings.\n\nIf you only sent regular data (not static files) and do not have high-end NIC with TLS offload, \nenabling Kernel TLS will only slightly decrease performance. CPU cost-wise it doesn't matter where encryption/decryption\nhappens, but the kernel `tls` module has to do extra bookkeeping. Also, aiofastnet can batch data and reduce amount of \nsyscalls when Kernel TLS is not used. \n\nKernel TLS requires support from all of these layers:\n\n- The `tls` kernel module loaded.\n- OpenSSL built with KTLS support on a machine with suitable kernel headers.\n- An `ssl.SSLContext` with `ssl.OP_ENABLE_KTLS` enabled.\n- A TLS version and cipher suite supported by the kernel TLS implementation.\n\nTo load the kernel module:\n\n```console\n$ sudo modprobe tls\n```\n\nTo enable KTLS on Python SSL contexts (available in Python 3.12+):\n\n```python\nimport ssl\n\nserver_context = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER)\nserver_context.options |= ssl.OP_ENABLE_KTLS\n\nclient_context = ssl.create_default_context()\nclient_context.options |= ssl.OP_ENABLE_KTLS\n```\n\nThe OpenSSL library used by Python must itself have KTLS support. Python\ndistributions often ship their own `libssl` and `libcrypto`, and those\nlibraries may have been built on older systems where KTLS was not available.\nThat can be true even when the host running your program has a newer kernel.\n\nCheck which OpenSSL Python is using:\n\n```console\n$ python -c \"import ssl; print(ssl.OPENSSL_VERSION); print(ssl._ssl.__file__)\"\n```\n\nIf your Python distribution bundles an older or non-KTLS OpenSSL, one practical\noption is to locate the bundled `libssl` and `libcrypto` files and replace them\nwith symbolic links to a newer system OpenSSL build that supports KTLS. This is\noften useful with Conda Python, which commonly ships its own OpenSSL libraries\ninside the environment.\n\nKTLS support by kernel version is outline [here.](https://delthas.fr/blog/2023/kernel-tls/)\n\nCheck out [aiohttp_ktls_fileresponse.py](https://github.com/tarasko/aiofastnet/blob/master/examples/aiohttp_ktls_fileresponse.py) and [aiohttp_ws_speedup.py](https://github.com/tarasko/aiofastnet/blob/master/examples/aiohttp_ws_speedup.py)  \nexamples showing how you can speed up aiohttp (or any other asyncio application).\n\nSome other useful links:\n* https://dev.to/ozkanpakdil/kernel-tls-nic-offload-and-socket-sharding-whats-new-and-who-uses-it-4e1f\n* https://www.kernel.org/doc/html/latest/networking/tls.html\n\n\n\n## Free-Threaded Python\n\n`aiofastnet` is compatible with free-threaded Python builds such as\n`python3.14t`. The extension modules are built to work without forcing the\nlegacy GIL back on, so separate event loops may run in separate threads.\n\nThe repository includes several free-threading examples:\n\n- [examples/benchmark_threaded.py](https://github.com/tarasko/aiofastnet/blob/master/examples/benchmark_threaded.py) runs multiple echo client/server pairs in\n  parallel to compare single-loop and multi-threaded execution.\n- [examples/echo_server_threaded.py](https://github.com/tarasko/aiofastnet/blob/master/examples/echo_server_threaded.py) starts one listening echo server per\n  thread on the same port with `reuse_port=True` so the kernel can distribute\n  incoming connections across worker threads.\n- [examples/echo_client_threaded.py](https://github.com/tarasko/aiofastnet/blob/master/examples/echo_client_threaded.py) starts one echo client per thread and\n  drives them in parallel against the shared server port.\n\nTransport objects remain thread-affine. Methods such as `write()`,\n`writelines()`, `close()`, `pause_reading()`, and similar transport\noperations must be called from the same thread that established the connection.\nCalling transport methods directly from a different thread raises\n`RuntimeError`.\n\nThis matches the general `asyncio` model: loops and loop-owned objects are\nnot meant to be used concurrently from arbitrary threads. `aiofastnet` does\nnot add internal transport locking for cross-thread access.\n\nTo use `aiofastnet` correctly with multithreading:\n\n- Create a separate event loop in each worker thread.\n- Create connections and servers inside the loop thread that will own them.\n- Keep all direct transport interaction on that same thread.\n- If another thread needs to send data or close a transport, schedule that work\n  onto the owning loop with `loop.call_soon_threadsafe(...)` instead of\n  touching the transport directly.\n\nIn other words, free-threaded Python lets multiple loops make progress in\nparallel, but each individual connection is still owned by exactly one loop and\none thread.\n\n## Building From Source\n\n1. Clone the repository:\n\n   ```console\n   $ git clone git@github.com:tarasko/aiofastnet.git\n   $ cd aiofastnet\n   ```\n\n2. Create and activate a virtual environment:\n\n   ```console\n   $ python3 -m venv aiofn-dev\n   $ source aiofn-dev/bin/activate\n   ```\n\n3. Install test dependencies:\n\n   ```console\n   $ pip install -r requirements-test.txt\n   ```\n\n4. Build extensions in place and run tests:\n\n   ```console\n   $ python setup.py build_ext --inplace\n   $ pytest -s -v\n   ```\n\n5. Run the benchmark:\n\n   ```console\n   $ python -m examples.benchmark\n   ```\n\n6. Run tests:\n\n   ```console\n   $ pytest -s -v\n   $ pytest -s -v -k test_echo[uvloop-tcp-buffered-6291456] --asyncio-debug --log-cli-level DEBUG\n   ```\n\n## Contributing\n\nContributions are welcome!\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftarasko%2Faiofastnet","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftarasko%2Faiofastnet","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftarasko%2Faiofastnet/lists"}