本文整理汇总了Python中nats.aio.client.Client.unsubscribe方法的典型用法代码示例。如果您正苦于以下问题:Python Client.unsubscribe方法的具体用法?Python Client.unsubscribe怎么用?Python Client.unsubscribe使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类nats.aio.client.Client
的用法示例。
在下文中一共展示了Client.unsubscribe方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_unsubscribe
# 需要导入模块: from nats.aio.client import Client [as 别名]
# 或者: from nats.aio.client.Client import unsubscribe [as 别名]
def test_unsubscribe(self):
nc = NATS()
msgs = []
@asyncio.coroutine
def subscription_handler(msg):
msgs.append(msg)
yield from nc.connect(io_loop=self.loop)
sid = yield from nc.subscribe("foo", cb=subscription_handler)
yield from nc.publish("foo", b'A')
yield from nc.publish("foo", b'B')
# Wait a bit to receive the messages
yield from asyncio.sleep(0.5, loop=self.loop)
self.assertEqual(2, len(msgs))
yield from nc.unsubscribe(sid)
yield from nc.publish("foo", b'C')
yield from nc.publish("foo", b'D')
# Ordering should be preserverd in these at least
self.assertEqual(b'A', msgs[0].data)
self.assertEqual(b'B', msgs[1].data)
# Should not exist by now
with self.assertRaises(KeyError):
nc._subs[sid].received
yield from asyncio.sleep(1, loop=self.loop)
endpoint = '127.0.0.1:{port}'.format(
port=self.server_pool[0].http_port)
httpclient = http.client.HTTPConnection(endpoint, timeout=5)
httpclient.request('GET', '/connz')
response = httpclient.getresponse()
connz = json.loads((response.read()).decode())
self.assertEqual(1, len(connz['connections']))
self.assertEqual(0, connz['connections'][0]['subscriptions'])
self.assertEqual(4, connz['connections'][0]['in_msgs'])
self.assertEqual(4, connz['connections'][0]['in_bytes'])
self.assertEqual(2, connz['connections'][0]['out_msgs'])
self.assertEqual(2, connz['connections'][0]['out_bytes'])
yield from nc.close()
self.assertEqual(2, nc.stats['in_msgs'])
self.assertEqual(2, nc.stats['in_bytes'])
self.assertEqual(4, nc.stats['out_msgs'])
self.assertEqual(4, nc.stats['out_bytes'])
示例2: Service
# 需要导入模块: from nats.aio.client import Client [as 别名]
# 或者: from nats.aio.client.Client import unsubscribe [as 别名]
#.........这里部分代码省略.........
def on_request(self, msg):
"""
message consumed treatment through provided callback and basic ack
"""
LOGGER.debug("natsd.Service.on_request - request " + str(msg) + " received")
try:
working_response = json.loads(msg.data.decode())
working_properties = DriverTools.json2properties(working_response['properties'])
working_body = b''+bytes(working_response['body'], 'utf8') if 'body' in working_response else None
working_body_decoded = base64.b64decode(working_body) if working_body is not None else \
bytes(json.dumps({}), 'utf8')
self.cb(working_properties, working_body_decoded)
except Exception as e:
LOGGER.warn("natsd.Service.on_request - Exception raised while treating msg {"+str(msg)+","+str(msg)+"}")
LOGGER.debug("natsd.Service.on_request - request " + str(msg) + " treated")
def connect(self):
LOGGER.debug("natsd.Service.connect")
try:
yield from self.nc.connect(**self.options)
self.serviceQS = yield from self.nc.subscribe(self.serviceQ, cb=self.on_request)
self.is_started = True
except ErrNoServers as e:
print(e)
return
def run_event_loop(self):
LOGGER.debug("natsd.Service.run_event_loop")
self.loop = asyncio.new_event_loop()
asyncio.set_event_loop(self.loop)
self.options = {
"servers": self.servers,
"name": self.name,
# "disconnected_cb": self.disconnected_cb,
# "reconnected_cb": self.reconnected_cb,
# "error_cb": self.error_cb,
# "closed_cb": self.closed_cb,
"io_loop": self.loop,
}
self.loop.create_task(self.connect())
self.loop.run_forever()
def on_start(self):
"""
start the service
"""
LOGGER.debug("natsd.Service.on_start")
self.service = threading.Thread(target=self.run_event_loop, name=self.serviceQ + " service thread")
self.service.start()
while not self.is_started:
time.sleep(0.01)
def on_stop(self):
"""
stop the service
"""
LOGGER.debug("natsd.Service.on_stop")
self.is_started = False
try:
next(self.nc.unsubscribe(self.serviceQS))
except StopIteration as e:
pass
try:
next(self.nc.close())
except StopIteration as e:
pass
try:
for task in asyncio.Task.all_tasks(self.loop):
task.cancel()
self.loop.stop()
while self.loop.is_running():
time.sleep(1)
self.loop.close()
except Exception as e:
LOGGER.debug("natsd.Service.on_stop - Exception aio clean up : "
+ traceback.format_exc())
def on_failure(self, exception_type, exception_value, traceback_):
LOGGER.error("natsd.Requester.on_failure - " + exception_type.__str__() + "/" + exception_value.__str__())
LOGGER.error("natsd.Requester.on_failure - " + traceback_.format_exc())
self.is_started = False
try:
next(self.nc.unsubscribe(self.serviceQS))
except StopIteration as e:
pass
try:
next(self.nc.close())
except StopIteration as e:
pass
try:
for task in asyncio.Task.all_tasks(self.loop):
task.cancel()
self.loop.stop()
while self.loop.is_running():
time.sleep(1)
self.loop.close()
except Exception as e:
LOGGER.debug("natsd.Service.on_failure - Exception aio clean up : "
+ traceback.format_exc())
示例3: Requester
# 需要导入模块: from nats.aio.client import Client [as 别名]
# 或者: from nats.aio.client.Client import unsubscribe [as 别名]
#.........这里部分代码省略.........
def run_event_loop(self):
LOGGER.debug("natsd.Requester.run_event_loop")
self.loop = asyncio.new_event_loop()
asyncio.set_event_loop(self.loop)
self.options = {
"servers": self.servers,
"name": self.name,
# "disconnected_cb": self.disconnected_cb,
# "reconnected_cb": self.reconnected_cb,
# "error_cb": self.error_cb,
# "closed_cb": self.closed_cb,
"io_loop": self.loop,
}
self.loop.create_task(self.connect())
self.loop.run_forever()
def on_start(self):
"""
start requester
"""
LOGGER.debug("natsd.Requester.on_start")
self.service = threading.Thread(target=self.run_event_loop, name=self.requestQ + " requestor thread")
self.service.start()
while not self.is_started:
time.sleep(0.01)
def on_stop(self):
"""
stop requester
"""
LOGGER.debug("natsd.Requester.on_stop")
self.is_started = False
try:
LOGGER.debug("natsd.Requester.on_stop - unsubscribe from " + str(self.responseQS))
next(self.nc.unsubscribe(self.responseQS))
except StopIteration as e:
pass
try:
LOGGER.debug("natsd.Requester.on_stop - close nats connection")
next(self.nc.close())
except StopIteration as e:
pass
LOGGER.debug("natsd.Requester.on_stop - nc is closed: " + str(self.nc.is_closed))
try:
LOGGER.debug("natsd.Requester.on_stop - cancelling aio tasks loop")
loop_to_stop = self.loop
for task in asyncio.Task.all_tasks(loop_to_stop):
LOGGER.debug("natsd.Requester.on_stop - cancelling task " + str(task))
task.cancel()
LOGGER.debug("natsd.Requester.on_stop - stopping aio loop stop")
loop_to_stop.stop()
count = 0
while loop_to_stop.is_running():
count += 1
if count % 10 == 0:
LOGGER.debug("natsd.Requester.on_stop - waiting aio loop to be stopped (" +
str(asyncio.Task.all_tasks(loop_to_stop).__len__()) + " tasks left; " +
"current task: " + str(asyncio.Task.current_task(loop_to_stop)) + ")")
for task in asyncio.Task.all_tasks(loop_to_stop):
LOGGER.debug("natsd.Requester.on_stop - cancelling task " + str(task))
task.cancel()
time.sleep(1)
if count == 120:
LOGGER.error("natsd.Requester.on_stop - unable to stop aio loop after 120 sec (" +
str(asyncio.Task.all_tasks(loop_to_stop).__len__()) + " tasks left; " +
"current task: " + str(asyncio.Task.current_task(loop_to_stop)) + ")")