RODY/app/services/RedisClient.py
552068321@qq.com 6f7de660aa first commit
2022-11-04 17:37:08 +08:00

47 lines
1.1 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"""
@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)