PUBLISH命令是Redis发布/订阅(pub/sub)功能的基础命令之一,常用于实现聊天室、实时消息推送等场景.下面就来详细讲解它的使用方法及示例:
PUBLISH channel message
channel:要发布消息的频道名称.
message:要发布的消息内容.
PUBLISH chat_room "Hello World!"
import redis
r = redis.Redis(host='localhost', port=6379, decode_responses=True)
r.publish('chat_room', 'Hello World!')
// 服务器端
import redis
import asyncio
async def handle(reader, writer):
await server.serve_forever()
asyncio.run(main())
// 客户端
import asyncio
import sys
async def tcp_echo_client():
await tcp_echo_client()
asyncio.run(main())
import redis
import time
r = redis.Redis(host='localhost', port=6379, decode_responses=True)
channel = 'product_update'
while True:
time.sleep(5)
以上就是土嘎嘎小编为大家整理的Redis PUBLISH命令相关主题介绍,如果您觉得小编更新的文章只要能对粉丝们有用,就是我们最大的鼓励和动力,不要忘记讲本站分享给您身边的朋友哦!!