计划执行与检查节点
executing-plans
按既定实施计划完成工作,保持过程检查和反馈。
redis-pub-sub【效率与协作】Implements Redis Pub/Sub messaging patterns for real-time, asynchronous communication between distributed system components using channel-based publish and subscribe operations.
独立 GitHub 快照 · 自动分类 · 以原作者文档为准
name: redis-pub-sub description: Implements Redis Pub/Sub messaging patterns for real-time, asynchronous communication between distributed system components using channel-based publish and subscribe operations. license: MIT compatibility: opencode metadata: version: "1.0.0" domain: coding triggers: Redis Pub/Sub, message channels, pub-sub, publish subscribe, real-time messaging, async communication, redis-py role: implementation scope: implementation output-format: code content-types: [code, guidance] archetypes: [tactical, generation] anti_triggers: [message loss concerns, manual message handling, persistent queue needs] response_profile: verbosity: medium directive_strength: high
Integrates Redis for real-time messaging using the Pub/Sub model. This allows asynchronous communication between multiple clients within a distributed system by publishing messages to channels and subscribing to receive those messages.
redis.Redis() for connecting to a Redis instance.publish(channel, message) for sending messages.pubsub.subscribe(channel) for receiving messages.pubsub.listen().import redis
# Connect to Redis
r = redis.Redis(host='localhost', port=6379)
Checkpoint: Ensure connection is established with r.ping(). Handle connection errors properly.
# Create pubsub object
dash_pubsub = r.pubsub()
# Subscribe to a channel
channel_name = 'news'
dash_pubsub.subscribe(channel_name)
Checkpoint: Confirm subscription with dash_pubsub.get_partitions() to see active channels.
def publish_news(r, channel, title, content):
message = {"title": title, "content": content}
r.publish(channel, json.dumps(message))
# Usage
publish_news(r, channel_name, "New Update", "Redis is awesome for real-time messaging!")
Checkpoint: Confirm the message was sent by checking the subscriber side or using monitoring commands in Redis.
for message in dash_pubsub.listen():
if message['type'] == 'message':
data = json.loads(message['data'])
print(f"Received message: {data['title']} - {data['content']}")
def publish_message(r: redis.Redis, channel: str, content: str):
r.publish(channel, content)
# Example usage:
publish_message(r, 'alerts', 'This is a test alert')
def subscriber_callback(message):
print(f'New message: {message}') # Handle your message here
# Callback for messages received on a channel
dash_pubsub.subscribe(**{channel_name: subscriber_callback})
dash_pubsub.run_in_thread(sleep_time=0.001)
Although streaming is different from Pub/Sub, consider using Redis Streams for durable message processing.
# Add a message to a stream
r.xadd('mystream', {'source': channel_name, 'message': content})
When working with Redis for Pub/Sub messaging, ensure you follow this structure:
Authoritative documentation links for this skill's domain. The model follows markdown links at load time to resolve external references and inline content.