Skip to main content

Python wrapper for libvalkey

Project description

libvalkey-py

Build Status License pypi

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.8+.

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, gets returns False. This means extra data is needed and feed should be called again before calling gets again. Alternatively you could provide custom sentinel object via parameter, which is useful for RESP3 protocol where native boolean types are supported:

Example:

>>> reader.feed("*2\r\n$5\r\nhello\r\n")
>>> reader.gets()
False
>>> reader.feed("$5\r\nworld\r\n")
>>> reader.gets()
[b'hello', b'world']
>>> reader = libvalkey.Reader(notEnoughData=Ellipsis)
>>> reader.gets()
Ellipsis

Unicode

libvalkey.Reader is able to decode bulk data to any encoding Python supports. To do so, specify the encoding you want to use for decoding replies when initializing it:

>>> reader = libvalkey.Reader(encoding="utf-8", errors="strict")
>>> reader.feed(b"$3\r\n\xe2\x98\x83\r\n")
>>> reader.gets()
'☃'

Decoding of bulk data will be attempted using the specified encoding and error handler. If the error handler is 'strict' (the default), a UnicodeDecodeError is raised when data cannot be dedcoded. This is identical to Python's default behavior. Other valid values to errors include 'replace', 'ignore', and 'backslashreplace'. More information on the behavior of these error handlers can be found here.

When the specified encoding cannot be found, a LookupError will be raised when calling gets for the first reply with bulk data.

Error handling

When a protocol error occurs (because of multiple threads using the same socket, or some other condition that causes a corrupt stream), the error libvalkey.ProtocolError is raised. Because the buffer is read in a lazy fashion, it will only be raised when gets is called and the first reply in the buffer contains an error. There is no way to recover from a faulty protocol state, so when this happens, the I/O code feeding data to Reader should probably reconnect.

The server can reply with error replies (-ERR ...). For these replies, the custom error class libvalkey.ReplyError is returned, but not raised.

When other error types should be used (so existing code doesn't have to change its except clauses), Reader can be initialized with the protocolError and replyError keywords. These keywords should contain a class that is a subclass of Exception. When not provided, Reader will use the default error types.

Benchmarks

The repository contains a benchmarking script in the benchmark directory, which uses gevent to have non-blocking I/O and valkey-py to handle connections. These benchmarks are done with a patched version of valkey-py that uses libvalkey-py when it is available.

All benchmarks are done with 10 concurrent connections.

  • SET key value + GET key
    • valkey-py: 11.76 Kops
    • valkey-py with libvalkey-py: 13.40 Kops
    • improvement: 1.1x

List entries in the following tests are 5 bytes.

  • LRANGE list 0 9:
    • valkey-py: 4.78 Kops
    • valkey-py with libvalkey-py: 12.94 Kops
    • improvement: 2.7x
  • LRANGE list 0 99:
    • valkey-py: 0.73 Kops
    • valkey-py with libvalkey-py: 11.90 Kops
    • improvement: 16.3x
  • LRANGE list 0 999:
    • valkey-py: 0.07 Kops
    • valkey-py with libvalkey-py: 5.83 Kops
    • improvement: 83.2x

Throughput improvement for simple SET/GET is minimal, but the larger multi bulk replies get, the larger the performance improvement is.

License

This code is released under the BSD license, as the license of hiredis-py at the time of fork.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

libvalkey-4.0.0.tar.gz (94.4 kB view hashes)

Uploaded Source

Built Distributions

libvalkey-4.0.0-pp310-pypy310_pp73-win_amd64.whl (21.3 kB view hashes)

Uploaded PyPy Windows x86-64

libvalkey-4.0.0-pp310-pypy310_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (49.0 kB view hashes)

Uploaded PyPy manylinux: glibc 2.17+ x86-64

libvalkey-4.0.0-pp310-pypy310_pp73-manylinux_2_17_i686.manylinux2014_i686.whl (56.4 kB view hashes)

Uploaded PyPy manylinux: glibc 2.17+ i686

libvalkey-4.0.0-pp310-pypy310_pp73-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (48.9 kB view hashes)

Uploaded PyPy manylinux: glibc 2.17+ ARM64

libvalkey-4.0.0-pp310-pypy310_pp73-macosx_11_0_arm64.whl (37.2 kB view hashes)

Uploaded PyPy macOS 11.0+ ARM64

libvalkey-4.0.0-pp310-pypy310_pp73-macosx_10_15_x86_64.whl (39.6 kB view hashes)

Uploaded PyPy macOS 10.15+ x86-64

libvalkey-4.0.0-pp39-pypy39_pp73-win_amd64.whl (21.3 kB view hashes)

Uploaded PyPy Windows x86-64

libvalkey-4.0.0-pp39-pypy39_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (49.0 kB view hashes)

Uploaded PyPy manylinux: glibc 2.17+ x86-64

libvalkey-4.0.0-pp39-pypy39_pp73-manylinux_2_17_i686.manylinux2014_i686.whl (56.3 kB view hashes)

Uploaded PyPy manylinux: glibc 2.17+ i686

libvalkey-4.0.0-pp39-pypy39_pp73-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (48.8 kB view hashes)

Uploaded PyPy manylinux: glibc 2.17+ ARM64

libvalkey-4.0.0-pp39-pypy39_pp73-macosx_11_0_arm64.whl (37.2 kB view hashes)

Uploaded PyPy macOS 11.0+ ARM64

libvalkey-4.0.0-pp39-pypy39_pp73-macosx_10_15_x86_64.whl (39.6 kB view hashes)

Uploaded PyPy macOS 10.15+ x86-64

libvalkey-4.0.0-pp38-pypy38_pp73-win_amd64.whl (21.3 kB view hashes)

Uploaded PyPy Windows x86-64

libvalkey-4.0.0-pp38-pypy38_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (49.1 kB view hashes)

Uploaded PyPy manylinux: glibc 2.17+ x86-64

libvalkey-4.0.0-pp38-pypy38_pp73-manylinux_2_17_i686.manylinux2014_i686.whl (56.5 kB view hashes)

Uploaded PyPy manylinux: glibc 2.17+ i686

libvalkey-4.0.0-pp38-pypy38_pp73-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (48.9 kB view hashes)

Uploaded PyPy manylinux: glibc 2.17+ ARM64

libvalkey-4.0.0-pp38-pypy38_pp73-macosx_11_0_arm64.whl (37.2 kB view hashes)

Uploaded PyPy macOS 11.0+ ARM64

libvalkey-4.0.0-pp38-pypy38_pp73-macosx_10_15_x86_64.whl (39.7 kB view hashes)

Uploaded PyPy macOS 10.15+ x86-64

libvalkey-4.0.0-cp312-cp312-win_amd64.whl (21.4 kB view hashes)

Uploaded CPython 3.12 Windows x86-64

libvalkey-4.0.0-cp312-cp312-win32.whl (19.6 kB view hashes)

Uploaded CPython 3.12 Windows x86

libvalkey-4.0.0-cp312-cp312-musllinux_1_2_x86_64.whl (167.9 kB view hashes)

Uploaded CPython 3.12 musllinux: musl 1.2+ x86-64

libvalkey-4.0.0-cp312-cp312-musllinux_1_2_s390x.whl (170.1 kB view hashes)

Uploaded CPython 3.12 musllinux: musl 1.2+ s390x

libvalkey-4.0.0-cp312-cp312-musllinux_1_2_ppc64le.whl (178.7 kB view hashes)

Uploaded CPython 3.12 musllinux: musl 1.2+ ppc64le

libvalkey-4.0.0-cp312-cp312-musllinux_1_2_i686.whl (164.9 kB view hashes)

Uploaded CPython 3.12 musllinux: musl 1.2+ i686

libvalkey-4.0.0-cp312-cp312-musllinux_1_2_aarch64.whl (166.9 kB view hashes)

Uploaded CPython 3.12 musllinux: musl 1.2+ ARM64

libvalkey-4.0.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (173.4 kB view hashes)

Uploaded CPython 3.12 manylinux: glibc 2.17+ x86-64

libvalkey-4.0.0-cp312-cp312-manylinux_2_17_s390x.manylinux2014_s390x.whl (173.2 kB view hashes)

Uploaded CPython 3.12 manylinux: glibc 2.17+ s390x

libvalkey-4.0.0-cp312-cp312-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl (184.2 kB view hashes)

Uploaded CPython 3.12 manylinux: glibc 2.17+ ppc64le

libvalkey-4.0.0-cp312-cp312-manylinux_2_17_i686.manylinux2014_i686.whl (167.7 kB view hashes)

Uploaded CPython 3.12 manylinux: glibc 2.17+ i686

libvalkey-4.0.0-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (173.0 kB view hashes)

Uploaded CPython 3.12 manylinux: glibc 2.17+ ARM64

libvalkey-4.0.0-cp312-cp312-macosx_11_0_arm64.whl (43.0 kB view hashes)

Uploaded CPython 3.12 macOS 11.0+ ARM64

libvalkey-4.0.0-cp312-cp312-macosx_10_15_x86_64.whl (44.8 kB view hashes)

Uploaded CPython 3.12 macOS 10.15+ x86-64

libvalkey-4.0.0-cp312-cp312-macosx_10_15_universal2.whl (82.2 kB view hashes)

Uploaded CPython 3.12 macOS 10.15+ universal2 (ARM64, x86-64)

libvalkey-4.0.0-cp311-cp311-win_amd64.whl (21.3 kB view hashes)

Uploaded CPython 3.11 Windows x86-64

libvalkey-4.0.0-cp311-cp311-win32.whl (19.5 kB view hashes)

Uploaded CPython 3.11 Windows x86

libvalkey-4.0.0-cp311-cp311-musllinux_1_2_x86_64.whl (165.3 kB view hashes)

Uploaded CPython 3.11 musllinux: musl 1.2+ x86-64

libvalkey-4.0.0-cp311-cp311-musllinux_1_2_s390x.whl (167.5 kB view hashes)

Uploaded CPython 3.11 musllinux: musl 1.2+ s390x

libvalkey-4.0.0-cp311-cp311-musllinux_1_2_ppc64le.whl (176.0 kB view hashes)

Uploaded CPython 3.11 musllinux: musl 1.2+ ppc64le

libvalkey-4.0.0-cp311-cp311-musllinux_1_2_i686.whl (162.7 kB view hashes)

Uploaded CPython 3.11 musllinux: musl 1.2+ i686

libvalkey-4.0.0-cp311-cp311-musllinux_1_2_aarch64.whl (164.6 kB view hashes)

Uploaded CPython 3.11 musllinux: musl 1.2+ ARM64

libvalkey-4.0.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (170.1 kB view hashes)

Uploaded CPython 3.11 manylinux: glibc 2.17+ x86-64

libvalkey-4.0.0-cp311-cp311-manylinux_2_17_s390x.manylinux2014_s390x.whl (170.4 kB view hashes)

Uploaded CPython 3.11 manylinux: glibc 2.17+ s390x

libvalkey-4.0.0-cp311-cp311-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl (181.7 kB view hashes)

Uploaded CPython 3.11 manylinux: glibc 2.17+ ppc64le

libvalkey-4.0.0-cp311-cp311-manylinux_2_17_i686.manylinux2014_i686.whl (165.1 kB view hashes)

Uploaded CPython 3.11 manylinux: glibc 2.17+ i686

libvalkey-4.0.0-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (169.8 kB view hashes)

Uploaded CPython 3.11 manylinux: glibc 2.17+ ARM64

libvalkey-4.0.0-cp311-cp311-macosx_11_0_arm64.whl (43.0 kB view hashes)

Uploaded CPython 3.11 macOS 11.0+ ARM64

libvalkey-4.0.0-cp311-cp311-macosx_10_15_x86_64.whl (44.7 kB view hashes)

Uploaded CPython 3.11 macOS 10.15+ x86-64

libvalkey-4.0.0-cp311-cp311-macosx_10_15_universal2.whl (82.0 kB view hashes)

Uploaded CPython 3.11 macOS 10.15+ universal2 (ARM64, x86-64)

libvalkey-4.0.0-cp310-cp310-win_amd64.whl (21.3 kB view hashes)

Uploaded CPython 3.10 Windows x86-64

libvalkey-4.0.0-cp310-cp310-win32.whl (19.5 kB view hashes)

Uploaded CPython 3.10 Windows x86

libvalkey-4.0.0-cp310-cp310-musllinux_1_2_x86_64.whl (165.2 kB view hashes)

Uploaded CPython 3.10 musllinux: musl 1.2+ x86-64

libvalkey-4.0.0-cp310-cp310-musllinux_1_2_s390x.whl (167.6 kB view hashes)

Uploaded CPython 3.10 musllinux: musl 1.2+ s390x

libvalkey-4.0.0-cp310-cp310-musllinux_1_2_ppc64le.whl (176.0 kB view hashes)

Uploaded CPython 3.10 musllinux: musl 1.2+ ppc64le

libvalkey-4.0.0-cp310-cp310-musllinux_1_2_i686.whl (162.6 kB view hashes)

Uploaded CPython 3.10 musllinux: musl 1.2+ i686

libvalkey-4.0.0-cp310-cp310-musllinux_1_2_aarch64.whl (164.5 kB view hashes)

Uploaded CPython 3.10 musllinux: musl 1.2+ ARM64

libvalkey-4.0.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (170.0 kB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.17+ x86-64

libvalkey-4.0.0-cp310-cp310-manylinux_2_17_s390x.manylinux2014_s390x.whl (170.3 kB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.17+ s390x

libvalkey-4.0.0-cp310-cp310-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl (181.6 kB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.17+ ppc64le

libvalkey-4.0.0-cp310-cp310-manylinux_2_17_i686.manylinux2014_i686.whl (165.3 kB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.17+ i686

libvalkey-4.0.0-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (169.8 kB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.17+ ARM64

libvalkey-4.0.0-cp310-cp310-macosx_11_0_arm64.whl (43.0 kB view hashes)

Uploaded CPython 3.10 macOS 11.0+ ARM64

libvalkey-4.0.0-cp310-cp310-macosx_10_15_x86_64.whl (44.7 kB view hashes)

Uploaded CPython 3.10 macOS 10.15+ x86-64

libvalkey-4.0.0-cp310-cp310-macosx_10_15_universal2.whl (82.0 kB view hashes)

Uploaded CPython 3.10 macOS 10.15+ universal2 (ARM64, x86-64)

libvalkey-4.0.0-cp39-cp39-win_amd64.whl (21.3 kB view hashes)

Uploaded CPython 3.9 Windows x86-64

libvalkey-4.0.0-cp39-cp39-win32.whl (19.5 kB view hashes)

Uploaded CPython 3.9 Windows x86

libvalkey-4.0.0-cp39-cp39-musllinux_1_2_x86_64.whl (164.7 kB view hashes)

Uploaded CPython 3.9 musllinux: musl 1.2+ x86-64

libvalkey-4.0.0-cp39-cp39-musllinux_1_2_s390x.whl (167.1 kB view hashes)

Uploaded CPython 3.9 musllinux: musl 1.2+ s390x

libvalkey-4.0.0-cp39-cp39-musllinux_1_2_ppc64le.whl (175.6 kB view hashes)

Uploaded CPython 3.9 musllinux: musl 1.2+ ppc64le

libvalkey-4.0.0-cp39-cp39-musllinux_1_2_i686.whl (162.1 kB view hashes)

Uploaded CPython 3.9 musllinux: musl 1.2+ i686

libvalkey-4.0.0-cp39-cp39-musllinux_1_2_aarch64.whl (164.0 kB view hashes)

Uploaded CPython 3.9 musllinux: musl 1.2+ ARM64

libvalkey-4.0.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (169.5 kB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.17+ x86-64

libvalkey-4.0.0-cp39-cp39-manylinux_2_17_s390x.manylinux2014_s390x.whl (169.8 kB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.17+ s390x

libvalkey-4.0.0-cp39-cp39-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl (181.0 kB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.17+ ppc64le

libvalkey-4.0.0-cp39-cp39-manylinux_2_17_i686.manylinux2014_i686.whl (164.7 kB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.17+ i686

libvalkey-4.0.0-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (169.2 kB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.17+ ARM64

libvalkey-4.0.0-cp39-cp39-macosx_11_0_arm64.whl (43.0 kB view hashes)

Uploaded CPython 3.9 macOS 11.0+ ARM64

libvalkey-4.0.0-cp39-cp39-macosx_10_15_x86_64.whl (44.7 kB view hashes)

Uploaded CPython 3.9 macOS 10.15+ x86-64

libvalkey-4.0.0-cp39-cp39-macosx_10_15_universal2.whl (82.0 kB view hashes)

Uploaded CPython 3.9 macOS 10.15+ universal2 (ARM64, x86-64)

libvalkey-4.0.0-cp38-cp38-win_amd64.whl (21.3 kB view hashes)

Uploaded CPython 3.8 Windows x86-64

libvalkey-4.0.0-cp38-cp38-win32.whl (19.5 kB view hashes)

Uploaded CPython 3.8 Windows x86

libvalkey-4.0.0-cp38-cp38-musllinux_1_2_x86_64.whl (166.0 kB view hashes)

Uploaded CPython 3.8 musllinux: musl 1.2+ x86-64

libvalkey-4.0.0-cp38-cp38-musllinux_1_2_s390x.whl (168.5 kB view hashes)

Uploaded CPython 3.8 musllinux: musl 1.2+ s390x

libvalkey-4.0.0-cp38-cp38-musllinux_1_2_ppc64le.whl (176.9 kB view hashes)

Uploaded CPython 3.8 musllinux: musl 1.2+ ppc64le

libvalkey-4.0.0-cp38-cp38-musllinux_1_2_i686.whl (163.4 kB view hashes)

Uploaded CPython 3.8 musllinux: musl 1.2+ i686

libvalkey-4.0.0-cp38-cp38-musllinux_1_2_aarch64.whl (165.4 kB view hashes)

Uploaded CPython 3.8 musllinux: musl 1.2+ ARM64

libvalkey-4.0.0-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (172.3 kB view hashes)

Uploaded CPython 3.8 manylinux: glibc 2.17+ x86-64

libvalkey-4.0.0-cp38-cp38-manylinux_2_17_s390x.manylinux2014_s390x.whl (172.7 kB view hashes)

Uploaded CPython 3.8 manylinux: glibc 2.17+ s390x

libvalkey-4.0.0-cp38-cp38-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl (183.7 kB view hashes)

Uploaded CPython 3.8 manylinux: glibc 2.17+ ppc64le

libvalkey-4.0.0-cp38-cp38-manylinux_2_17_i686.manylinux2014_i686.whl (166.9 kB view hashes)

Uploaded CPython 3.8 manylinux: glibc 2.17+ i686

libvalkey-4.0.0-cp38-cp38-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (172.3 kB view hashes)

Uploaded CPython 3.8 manylinux: glibc 2.17+ ARM64

libvalkey-4.0.0-cp38-cp38-macosx_11_0_arm64.whl (43.0 kB view hashes)

Uploaded CPython 3.8 macOS 11.0+ ARM64

libvalkey-4.0.0-cp38-cp38-macosx_10_15_x86_64.whl (44.8 kB view hashes)

Uploaded CPython 3.8 macOS 10.15+ x86-64

libvalkey-4.0.0-cp38-cp38-macosx_10_15_universal2.whl (82.1 kB view hashes)

Uploaded CPython 3.8 macOS 10.15+ universal2 (ARM64, x86-64)

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page