#!/usr/bin/env python
# -*- coding:utf-8 -*-
import redis


class RedisHelper:

    def __init__(self, host1, port1, password1):
        # 链接服务端        
        self.__conn = redis.Redis(host=host1, port=port1, password=password1)

    def public(self, channel_public, msg):
        # 发消息订阅方
        # publish发消息加入频道chan_pub
        self.__conn.publish(channel_public, msg)
        
    def getConn(self):
        return self.__conn
    def subscribe(self, channel_subscribe):
        # 开始订阅pubsub()
        # 打开收音机
        pub = self.__conn.pubsub()

        # 调频道 subscribe
        pub.subscribe(channel_subscribe)

        # 准备接收parse_response()
        # 在次调用parse_response() 开始接收
        pub.parse_response()

        # 返回订阅变量
        return pub

# obj = RedisHelper()

# # 赋值订阅变量
# redis_sub = obj.subscribe('kyevent')
# # obj.public('kyevent', 'hello')
# # # 循环执行如下命令
# while True:
#     # 二次调用parse_response() 开始接收
#     msg = redis_sub.parse_response()
#     pycomm.toLog(msg)
