当前位置: 首页>>代码示例>>Python>>正文


Python Channel.read方法代码示例

本文整理汇总了Python中channel.Channel.read方法的典型用法代码示例。如果您正苦于以下问题:Python Channel.read方法的具体用法?Python Channel.read怎么用?Python Channel.read使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在channel.Channel的用法示例。


在下文中一共展示了Channel.read方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: MQStar

# 需要导入模块: from channel import Channel [as 别名]
# 或者: from channel.Channel import read [as 别名]
class MQStar():
    """ MQStar is a Message Queue with a star topology. There's a server and multiple clients.
    Each mqstar is identified by a session, which is the name of the underlining redis channels.
    A server can send messages to clients on different channels, but receives answers on one channel: channel_to_server

    """
    session = ""
    channels = {}
    """MQStar is a MessageQueue with a star topology based on Redis"""

    def __init__(self, host, session=None):
        self.host = host
        if not session:
            self.session = id_generator()
        else:
            self.session = session

        self.set_session(session)

    def set_session(self, session):
        channel_server = "MQ_%s_to_server" % self.session
        self.channel_to_server = Channel(self.host, channel_server)

    def _make_channel(self, frm="server", to="server"):
        name = "MQ_%s_%s_%s" % (self.session, frm, to)
        channel = Channel(self.host, name)
        return channel

    def notify_connection(self, av):
        logging.debug("notify_connection %s" % av)
        redis = self.channel_to_server.redis
        name = "MQ_%s_connection" % (self.session)
        redis.sadd(name,av)

    def reset_connection(self, av):
        logging.debug("reset_connection %s" % av)
        redis = self.channel_to_server.redis
        name = "MQ_%s_connection" % (self.session)
        redis.srem(name,av)

    def check_connection(self, av):
        logging.debug("check_connection %s" % av)
        redis = self.channel_to_server.redis
        name = "MQ_%s_connection" % (self.session)
        return redis.sismember(name,av)

    def clean(self, av=None):
        """ Cleans all the redis keys related to the used channels
        """
        if av:
            key = "MQ_*_server_%s" % av
        else:
            key = "MQ_*"

        for k in self.channel_to_server.redis.keys(key):
            logging.debug(" MQ clean %s" % k)
            self.channel_to_server.redis.delete(k)

            #assert not self.channel_to_server.redis.keys("MQ_*")

    def add_client(self, client):
        if client not in self.channels.keys():
            ch = self._make_channel(to=client)
            #chRight = self.channelToServer
            self.channels[client] = ch

    def add_clients(self, clients):
        for c in clients:
            self.add_client(c)

    def send_server(self, client, message):
        if client not in self.channels.keys():
            logging.debug(" MQ error, client not found")
        ch = self.channel_to_server
        payload = (client, message)
        ch.write(payload)

    def receive_server(self, blocking=False, timeout=10):
        #logging.debug(" MQ receive_server")
        payload = self.channel_to_server.read(blocking, timeout)

        if not payload:
            logging.error("TIMEOUT")
            return None

        p = re.compile("\('(\w+)', (.+)\)")
        m = p.match(payload)
        assert m, "wrong format: %s" % m

        cmd, args = m.group(1), m.group(2)
        #logging.debug(" MQ read: %s args: %s" % (str(cmd), str(args)))
        #client, message = payload
        return cmd, args

    def send_client(self, client, message):
        if client not in self.channels.keys():
            logging.debug(" MQ error, sendClient, client not found: %s" %
                          self.channels)
        ch = self.channels[client]
        ch.write(message)
#.........这里部分代码省略.........
开发者ID:BwRy,项目名称:test-av2,代码行数:103,代码来源:mq.py


注:本文中的channel.Channel.read方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。