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

42 lines
941 B
Python
Raw 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 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()