Skip to main content

一个python redis 的简易分布式消息实现

Project description

使用方法(基于 Redis Streams 的分布式生产/消费)

一、环境准备

  • 操作系统:Windows 11
  • 依赖管理与执行:uv(禁止直接使用 python 命令)
  • Redis:确保可访问(默认示例使用 192.168.2.251:6379,db=6,密码 redis_Q8zTm7)

二、启动生产者(一个终端)

  • 命令: uv run src/rickcommons/main.py --topic test_topic --key-prefix test_key --interval 1,3 --host 192.168.2.251 --port 6379 --db 6 --password redis_Q8zTm7
  • 说明:
    • 将消息写入指定主题(topic)的 Redis Stream(XADD)。
    • 每 1–3 秒发送一条消息,Hash 持久化键形如 test_key_时间戳。

三、启动消费者 A(第二个终端)

  • 命令: uv run src/rickcommons/client.py --topic test_topic --group group_A --timeout 1s --host 192.168.2.251 --port 6379 --db 6 --password redis_Q8zTm7
  • 说明:
    • 从消费者组 group_A 读取消息(XREADGROUP),收到后打印并确认(XACK)。

四、启动消费者 B(第三个终端)

  • 命令: uv run src/rickcommons/client.py --topic test_topic --group group_B --timeout 1s --host 192.168.2.251 --port 6379 --db 6 --password redis_Q8zTm7
  • 说明:
    • 与消费者 A 订阅同一主题但不同组。两者将分别收到同一条消息,实现“同一消息被不同消费者处理(跨组广播)”。

五、参数快速说明(常用)

  • --topic:消息主题(Stream 名称),如 test_topic。
  • --group:消费者组名称。不同组之间相互独立,能够各自获取到同一条消息;同一组内多个实例会对消息做负载均衡。
  • --timeout:阻塞超时,支持 1000、"1s"、"200ms" 等格式。
  • --key-prefix(生产者):Hash 持久化键前缀。
  • --interval(生产者):发送间隔范围,格式 "低,高",如 "1,3"。
  • --host/--port/--db/--password:Redis 连接参数。

六、退出

  • 按 Ctrl+C 停止对应终端的生产者或消费者进程。

七、提示

  • 想要横向扩展同一类处理逻辑:在同一个组内启动多个消费者实例(不同 consumer_id),实现组内负载均衡。
  • 想要让同一消息被不同处理器独立处理:为同一主题创建不同的组(如 group_A、group_B),实现跨组广播。

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

rickcommons-1.0.0.tar.gz (11.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

rickcommons-1.0.0-py3-none-any.whl (12.4 kB view details)

Uploaded Python 3

File details

Details for the file rickcommons-1.0.0.tar.gz.

File metadata

  • Download URL: rickcommons-1.0.0.tar.gz
  • Upload date:
  • Size: 11.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.7.8

File hashes

Hashes for rickcommons-1.0.0.tar.gz
Algorithm Hash digest
SHA256 c87c85e64809014973f49231b69017917bc1ee7fd043eeb40d04f73a6572a012
MD5 3ee722a790b0b8621862b1cac3d53232
BLAKE2b-256 4ecd8f9bbce030460b7ef10f44b7d3ace1862a54d3cfa784ed7a3ba78d00d3cc

See more details on using hashes here.

File details

Details for the file rickcommons-1.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for rickcommons-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 79e027b8c525703eb543e52451963b53f23cd966e9a5ca6464098c94d2e629db
MD5 5b7a43131e63c3856d5c4b7ee3d07411
BLAKE2b-256 1ee71e26aef3934186619a18a09884854398d2d89409aabc309d460aab10fdf4

See more details on using hashes here.

Supported by

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