wsaccel: WebSocket Accelerator
wsaccel is a Python library that provides a WebSocket accelerator for `ws4py` and `AutobahnPython`. It replaces per-byte processing in these libraries with a faster Cython implementation, primarily for UTF-8 validation and payload masking. The project is currently at version 0.6.7, but its maintainer has indicated that it will cease active development due to its dependencies (ws4py and AutobahnPython) no longer being actively maintained. Users are advised to migrate to more actively supported WebSocket libraries like Tornado or websockets.
Common errors
-
ImportError: /builddir/build/BUILD/wsaccel-0.6.2/tests/test_4.py:2: in import wsaccel.utf8validator. E ImportError: /builddir/...
cause This `ImportError` during build or test is typically caused by `wsaccel`'s use of deprecated Python C API buffer protocols (e.g., `PyObject_AsReadBuffer`) which were removed in Python 3.10.fixThis issue indicates incompatibility with Python 3.10+. If you must use `wsaccel`, use Python 3.9 or earlier. The long-term solution is to migrate your application to a different, actively maintained WebSocket library that supports newer Python versions. -
My WebSocket server/client is slow, especially with large text messages.
cause Native Python implementations of UTF-8 validation and payload masking in libraries like `ws4py` and `AutobahnPython` can be CPU-intensive, leading to performance bottlenecks, particularly with frequent or large text-based WebSocket messages.fixEnsure `wsaccel` is installed and correctly patched into your `ws4py` or `AutobahnPython` application (e.g., `wsaccel.patch_ws4py()`). If using `websocket-client`, `wsaccel` is used automatically if available. This offloads the intensive operations to a faster Cython implementation.
Warnings
- deprecated The wsaccel project is explicitly marked by its maintainer as no longer actively maintained. Users are strongly advised to migrate to modern, actively maintained WebSocket libraries like Tornado or websockets.
- breaking Building `wsaccel` with Python 3.10 and newer may fail due to the removal of the deprecated Python buffer protocol API (e.g., `PyObject_AsReadBuffer()`). This can lead to `ImportError` or build failures.
- gotcha Installing `wsaccel` alone does not provide any functionality. It acts as an accelerator for other WebSocket libraries. If those libraries are not installed or explicitly patched (for `ws4py`/`AutobahnPython`), `wsaccel` will have no effect.
Install
-
pip install wsaccel
Imports
- wsaccel
from wsaccel import Utf8Validator
import wsaccel wsaccel.patch_autobahn() # Or wsaccel.patch_ws4py()
Quickstart
import wsaccel
# To accelerate AutobahnPython:
# import autobahn.websocket
# wsaccel.patch_autobahn()
# print('AutobahnPython patched by wsaccel.')
# To accelerate ws4py:
# import ws4py.server.cherrypyserver
# wsaccel.patch_ws4py()
# print('ws4py patched by wsaccel.')
print('wsaccel imported. Manual patching for target libraries is required if not automatically detected.')