本文整理汇总了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()