libvalkey

4.0.1 · active · verified Fri Apr 10

libvalkey-py is a Python extension that wraps the protocol parsing code from the underlying libvalkey C library. It primarily speeds up the parsing of multi bulk replies from a Valkey server. The library is actively maintained, with its current version being 4.0.1, and releases occurring regularly to introduce new features, improvements, and bug fixes.

Warnings

Install

Imports

Quickstart

The quickstart demonstrates how to initialize the `Reader` class, feed raw Valkey protocol bytes, and retrieve parsed replies. It also shows how to handle cases where insufficient data is fed, using a custom sentinel value.

import libvalkey

# The Reader class is responsible for parsing replies from a stream of data.
# It does not handle I/O itself.
reader = libvalkey.Reader()

# Feed raw Valkey protocol bytes to the reader
reader.feed(b"$5\r\nhello\r\n")

# Retrieve the parsed reply
reply = reader.gets()
print(f"Parsed reply: {reply}")

# Example with a list reply
reader.feed(b"*2\r\n$5\r\nhello\r\n$5\r\nworld\r\n")
list_reply = reader.gets()
print(f"Parsed list reply: {list_reply}")

# Example with custom sentinel for 'not enough data'
reader_ellipsis = libvalkey.Reader(notEnoughData=Ellipsis)
reader_ellipsis.feed(b"*2\r\n$5\r\nhello\r\n")
partial_reply = reader_ellipsis.gets()
print(f"Partial reply (needs more data): {partial_reply}")
reader_ellipsis.feed(b"$5\r\nworld\r\n")
full_reply = reader_ellipsis.gets()
print(f"Full reply: {full_reply}")

view raw JSON →