Add example scripts for common Redis operations#4032
Add example scripts for common Redis operations#4032yosofbadr wants to merge 3 commits intoredis:masterfrom
Conversation
Add an examples/ directory with three self-contained scripts covering the most common redis-py usage patterns: - string_operations.py: SET/GET, MSET/MGET, atomic counters, key expiry - hash_operations.py: HSET/HGET/HGETALL, HMGET, HINCRBY, field inspection - pubsub.py: SUBSCRIBE, PSUBSCRIBE, PUBLISH with a threaded demo Closes redis#1744
|
Hi, I’m Jit, a friendly security platform designed to help developers build secure applications from day zero with an MVS (Minimal viable security) mindset. In case there are security findings, they will be communicated to you as a comment inside the PR. Hope you’ll enjoy using Jit. Questions? Comments? Want to learn more? Get in touch with us. |
|
Hi @yosofbadr, thank you for your contribution! In redis-py, examples are stored under docs/examples as Jupyter notebooks and are then referenced in the Read the Docs documentation. Could you please update the examples you added to follow this existing pattern? |
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 1 potential issue.
Reviewed by Cursor Bugbot for commit c6999f4. Configure here.
| pub_thread.start() | ||
|
|
||
| pub_thread.join() | ||
| sub_thread.join() |
There was a problem hiding this comment.
Subscriber thread hangs forever if publisher fails
Medium Severity
The sub_thread is a non-daemon thread blocking on pubsub.listen(), which only terminates when the "QUIT" sentinel arrives. If the publisher thread raises an exception (e.g., a connection error) before sending "QUIT", the subscriber blocks indefinitely and sub_thread.join() in main() hangs the process. Because the thread is not set as a daemon, Ctrl+C also fails to terminate the process — the main thread receives KeyboardInterrupt, but Python waits for the non-daemon subscriber thread, which remains stuck in the blocking socket read. Setting sub_thread.daemon = True or using get_message(timeout=...) in a loop instead of listen() would prevent the hang.
Additional Locations (1)
Reviewed by Cursor Bugbot for commit c6999f4. Configure here.


Summary
examples/directory with self-contained, well-commented scripts demonstrating common redis-py usage patternsstring_operations.py-- SET/GET, MSET/MGET, atomic counters (INCR/DECR), key expiration with EX/NX/XX flagshash_operations.py-- HSET/HGET/HGETALL, HMGET, HINCRBY, HEXISTS/HKEYS/HVALS/HDELpubsub.py-- SUBSCRIBE, PSUBSCRIBE (pattern-based), PUBLISH with a threaded demo that runs subscriber and publisher in one processEach script includes a docstring with prerequisites (pip install, Docker command to start Redis), cleans up after itself, and can be run standalone.
Closes #1744
Test plan
Note
Low Risk
Low risk: this PR only adds standalone example scripts under
examples/and does not modify library/runtime code paths.Overview
Adds a new
examples/set of standalone, runnable Python scripts demonstrating commonredis-pyusage.Includes examples for string operations (SET/GET, bulk MSET/MGET, counters, expirations/flags), hash operations (HSET/HGET/HGETALL, HMGET, HINCRBY, field inspection/deletion), and a pub/sub demo that runs a subscriber and publisher in threads and exits cleanly via a sentinel message; each script documents prerequisites and cleans up created keys where applicable.
Reviewed by Cursor Bugbot for commit c6999f4. Bugbot is set up for automated code reviews on this repo. Configure here.