RODY/app/utils/RedisMQTool.py

42 lines
941 B
Python
Raw Normal View History

2022-11-04 17:37:08 +08:00
"""
@Time 2022/10/9 17:50
@Auth
@File RedisMQTool.py
@IDE PyCharm
@MottoABC(Always Be Coding)
@Desc
"""
import json
import redis
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 publish_task(self, data):
self.rcon.publish(self.key, json.dumps(data))
def del_listen(self):
self.ps.unsubscribe(self.key)
if __name__ == '__main__':
print("listen task channel")
pool = redis.ConnectionPool(host='127.0.0.1',
port=6379, db=5,)
redis_conn = redis.StrictRedis(connection_pool=pool)
Task(redis_conn, 'channel').listen_task()