"""
@Time : 2022/10/9 17:50
@Auth : 东
@File :RedisMQTool.py
@IDE :PyCharm
@Motto:ABC(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()