close
Skip to content

Add example scripts for common Redis operations#4032

Open
yosofbadr wants to merge 3 commits intoredis:masterfrom
yosofbadr:docs/add-examples
Open

Add example scripts for common Redis operations#4032
yosofbadr wants to merge 3 commits intoredis:masterfrom
yosofbadr:docs/add-examples

Conversation

@yosofbadr
Copy link
Copy Markdown

@yosofbadr yosofbadr commented Apr 15, 2026

Summary

  • Adds a new examples/ directory with self-contained, well-commented scripts demonstrating common redis-py usage patterns
  • string_operations.py -- SET/GET, MSET/MGET, atomic counters (INCR/DECR), key expiration with EX/NX/XX flags
  • hash_operations.py -- HSET/HGET/HGETALL, HMGET, HINCRBY, HEXISTS/HKEYS/HVALS/HDEL
  • pubsub.py -- SUBSCRIBE, PSUBSCRIBE (pattern-based), PUBLISH with a threaded demo that runs subscriber and publisher in one process

Each 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

  • Run each script against a local Redis instance and verify output
  • Confirm scripts clean up all keys they create
  • Verify the pub/sub script exits cleanly (no hanging threads)

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 common redis-py usage.

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.

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
@jit-ci
Copy link
Copy Markdown

jit-ci Bot commented Apr 15, 2026

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.

@yosofbadr yosofbadr marked this pull request as ready for review April 15, 2026 20:21
Comment thread examples/pubsub.py Outdated
@petyaslavova
Copy link
Copy Markdown
Collaborator

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?

Copy link
Copy Markdown

@cursor cursor Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

Reviewed by Cursor Bugbot for commit c6999f4. Configure here.

Comment thread examples/pubsub.py
pub_thread.start()

pub_thread.join()
sub_thread.join()
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)
Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit c6999f4. Configure here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

We need examples!

2 participants