Python wrapper for libvalkey
Actively maintained. Last release 7 weeks ago, commits in 8 of the last 26 weeks. A safe choice.
pip install libvalkeylibvalkey-py
Python extension that wraps protocol parsing code in libvalkey. It primarily speeds up parsing of multi bulk replies.
Install
libvalkey-py is available on PyPI, and can be installed via:
pip install libvalkey
Building and Testing
Building this repository requires a recursive checkout of submodules, and building libvalkey. The following example shows how to clone, compile, and run tests. Please note - you will need the gcc installed.
git clone --recurse-submodules https://github.com/valkey-io/libvalkey-py
python setup.py build_ext --inplace
python -m pytest
Requirements
libvalkey-py requires Python 3.11+.
Make sure Python development headers are available when installing libvalkey-py.
On Ubuntu/Debian systems, install them with apt-get install python3-dev.
Usage
The libvalkey module contains the Reader class. This class is responsible for
parsing replies from the stream of data that is read from a Redis connection.
It does not contain functionality to handle I/O.
Reply parser
The Reader class has two methods that are used when parsing replies from a
stream of data. Reader.feed takes a string argument that is appended to the
internal buffer. Reader.gets reads this buffer and returns a reply when the
buffer contains a full reply. If a single call to feed contains multiple
replies, gets should be called multiple times to extract all replies.
Example:
>>> reader = libvalkey.Reader()
>>> reader.feed("$5\r\nhello\r\n")
>>> reader.gets()
b'hello'
When the buffer does not contain a full reply, returns . This means extra data is needed and should be called again before calling again. Alternatively you could provide custom sentinel object via parameter, which is useful for RESP3 protocol where native boolean types are supported: