RODY/app/services/RedisClient.py

47 lines
1.1 KiB
Python
Raw Normal View History

2022-11-04 17:37:08 +08:00
"""
@Time 2022/10/9 11:53
@Auth
@File RedisClient.py
@IDE PyCharm
@MottoABC(Always Be Coding)
@Desc
"""
# coding:utf-8
import time
import redis
def redisClient():
rc = redis.StrictRedis(host="localhost", port="6379", db=0, password="sdust2020")
ps = rc.pubsub()
ps.subscribe("liao") # 订阅消息
a = 0
for item in ps.listen(): # 监听状态:有消息发布了就拿过来
print(item)
data = item['data']
if type(data) == bytes:
data = item['data'].decode()
print(data)
if data == '300030 -1':
ps.unsubscribe("liao")
print(1)
class Task(object):
def __init__(self, redis_conn, channel):
self.rcon = redis_conn
self.ps = self.rcon.pubsub()
self.key = 'task:pubsub:%s' % channel
self.ps.subscribe(self.key)
def listen_task(self):
for i in self.ps.listen():
if i['type'] == 'message':
print("Task get ", i["data"])
def del_listen(self):
self.ps.unsubscribe(self.key)