Hiredis Python Wrapper

3.3.1 · active · verified Sun Apr 05

Hiredis is a Python wrapper for the minimalistic C client library for Redis, also named hiredis. It provides a fast, compiled response parser, significantly speeding up the parsing of Redis replies, especially multi-bulk responses. It is primarily used to boost the performance of Python Redis clients like `redis-py` by providing a faster parser. The current version is 3.3.1 and it maintains an active release cadence with frequent compatibility updates and bug fixes.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates the direct usage of `hiredis.Reader` to parse Redis protocol strings. The `feed()` method takes raw bytes, and `gets()` retrieves the next complete parsed reply. It handles different Redis data types and returns `False` if a complete reply is not yet available in the buffer.

import hiredis

# Create a Reader instance to parse Redis protocol data
reader = hiredis.Reader()

# Example 1: Parse a simple string reply ('+OK\r\n')
reader.feed(b"+OK\r\n")
result_ok = reader.gets()
print(f"Parsed simple string: {result_ok}")

# Example 2: Parse a bulk string reply ('$5\r\nhello\r\n')
reader.feed(b"$5\r\nhello\r\n")
result_hello = reader.gets()
print(f"Parsed bulk string: {result_hello}")

# Example 3: Parse an array reply ('*2\r\n$5\r\nhello\r\n$5\r\nworld\r\n')
reader.feed(b"*2\r\n$5\r\nhello\r\n$5\r\nworld\r\n")
result_array = reader.gets()
print(f"Parsed array: {result_array}")

# Example 4: Handle incomplete data - gets() returns False if no full reply is available
reader.feed(b"$6\r\nfoobar")
incomplete_result = reader.gets()
print(f"Incomplete data (should be False): {incomplete_result}")
reader.feed(b"\r\n") # Complete the data
completed_result = reader.gets()
print(f"Completed data: {completed_result}")

view raw JSON →