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


Python Handler.cleanup方法代码示例

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


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

示例1: SocketPattern

# 需要导入模块: from handler import Handler [as 别名]
# 或者: from handler.Handler import cleanup [as 别名]
class SocketPattern(object):
    def __init__(self, socket_type, num_data_workers=0):
        """
        Base class for socket patterns. This should not be instanstiated directly.

        :param socket_type: the zmq socket type to create, e.g zmq.REP.
        :type socket_type: int.
        :param num_data_workers: number of worker threads used to handle callbacks,
                                 defaults to 0 which means callbacks are synchronous.
        :type num_data_workers: int.
        """

        self._address = None
        self.context = zmq.Context()
        self.socket = self.context.socket(socket_type)


        self.action_map = {}
        self.handler = Handler(self.action_map, num_data_workers)

        self.event_stream = zmqstream.ZMQStream(self.socket)

    @property
    def address(self):
        """
        Address the underlying socket is bound or connected to. Will raise zmq.ZMQError
        if accessed before a call to bind or connect.

        :raises: zmq.ZMQerror
        """
        if not self._address:
            raise zmq.ZMQError('Address not available, socket not bound or connected!')
        return self._address

    def bind(self, address):
        """
        Binds the underlying socket to address.

        :param address: Address to bind the socket to. Address is made up of a protocol,
                        location and port, e.g:
                        inproc://thread_location
                        ipc://process_location
                        tcp://127.0.0.1:5555
        :type address: str.
        """

        self.socket.bind(address)
        self._address = address

    def connect(self, address):
        """
        Connects the underlying socket to address.

        :param address: Address to bind the socket to. Address is made up of a protocol,
                        location and port, e.g:
                        inproc://thread_location
                        ipc://process_location
                        tcp://127.0.0.1:5555
        :type address: str.
        """
        self.socket.connect(address)
        self._address = address

    def recv_json(self):
        """
        Receive a JSON message and pass it on to the dispatch function. This blocks
        until a message is received.
        """
        return self.on_recv(self.socket.recv_multipart())

    def on_recv(self, msg):
        """
        Take a message and pass the data on to the handler class.

        :param msg: A list of length two. The first item is an action string, the second
                    is the data string which is serialized to JSON.
        :type msg: list.
        """
        action, data = msg
        data = json.loads(data)
        self.handler(data)
        return action, data

    def cleanup(self):
        """
        Cleanup and destroy zmq context. This stop data handler threads processing
        further actions.
        """
        self.socket.close()
        self.handler.cleanup()
        self.context.destroy()
开发者ID:ahal,项目名称:corredor,代码行数:93,代码来源:pattern.py


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