本文整理汇总了Python中nats.aio.client.Client.request方法的典型用法代码示例。如果您正苦于以下问题:Python Client.request方法的具体用法?Python Client.request怎么用?Python Client.request使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类nats.aio.client.Client
的用法示例。
在下文中一共展示了Client.request方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_new_style_request
# 需要导入模块: from nats.aio.client import Client [as 别名]
# 或者: from nats.aio.client.Client import request [as 别名]
def test_new_style_request(self):
nc = NATS()
msgs = []
counter = 0
@asyncio.coroutine
def worker_handler(msg):
nonlocal counter
counter += 1
msgs.append(msg)
yield from nc.publish(msg.reply, 'Reply:{}'.format(counter).encode())
@asyncio.coroutine
def slow_worker_handler(msg):
yield from asyncio.sleep(0.5, loop=self.loop)
yield from nc.publish(msg.reply, b'timeout by now...')
yield from nc.connect(io_loop=self.loop)
yield from nc.subscribe("help", cb=worker_handler)
yield from nc.subscribe("slow.help", cb=slow_worker_handler)
response = yield from nc.request("help", b'please', timeout=1)
self.assertEqual(b'Reply:1', response.data)
response = yield from nc.request("help", b'please', timeout=1)
self.assertEqual(b'Reply:2', response.data)
with self.assertRaises(ErrTimeout):
yield from nc.request("slow.help", b'please', timeout=0.1)
yield from asyncio.sleep(1, loop=self.loop)
yield from nc.close()
示例2: main
# 需要导入模块: from nats.aio.client import Client [as 别名]
# 或者: from nats.aio.client.Client import request [as 别名]
def main(loop):
parser = argparse.ArgumentParser()
parser.add_argument('-n', '--iterations', default=DEFAULT_ITERATIONS, type=int)
parser.add_argument('-S', '--subject', default='test')
parser.add_argument('--servers', default=[], action='append')
args = parser.parse_args()
servers = args.servers
if len(args.servers) < 1:
servers = ["nats://127.0.0.1:4222"]
opts = { "servers": servers }
# Make sure we're connected to a server first...
nc = NATS()
try:
yield from nc.connect(**opts)
except Exception as e:
sys.stderr.write("ERROR: {0}".format(e))
show_usage_and_die()
@asyncio.coroutine
def handler(msg):
yield from nc.publish(msg.reply, b'')
yield from nc.subscribe(args.subject, cb=handler)
# Start the benchmark
start = time.monotonic()
to_send = args.iterations
print("Sending {0} request/responses on [{1}]".format(
args.iterations, args.subject))
while to_send > 0:
to_send -= 1
if to_send == 0:
break
yield from nc.request(args.subject, b'')
if (to_send % HASH_MODULO) == 0:
sys.stdout.write("#")
sys.stdout.flush()
duration = time.monotonic() - start
ms = "%.3f" % ((duration/args.iterations) * 1000)
print("\nTest completed : {0} ms avg request/response latency".format(ms))
yield from nc.close()
示例3: run
# 需要导入模块: from nats.aio.client import Client [as 别名]
# 或者: from nats.aio.client.Client import request [as 别名]
def run(loop):
nc = NATS()
try:
yield from nc.connect(io_loop=loop)
except ErrNoServers as e:
print(e)
return
@asyncio.coroutine
def message_handler(msg):
subject = msg.subject
reply = msg.reply
data = msg.data.decode()
for i in range(0, 20):
yield from nc.publish(reply, "i={i}".format(i=i).encode())
yield from nc.subscribe("help.>", cb=message_handler)
@asyncio.coroutine
def request_handler(msg):
subject = msg.subject
reply = msg.reply
data = msg.data.decode()
print("Received a message on '{subject} {reply}': {data}".format(
subject=subject, reply=reply, data=data))
# Signal the server to stop sending messages after we got 10 already.
yield from nc.request("help.please", b'help', expected=10, cb=request_handler)
try:
# Flush connection to server, returns when all messages have been processed.
# It raises a timeout if roundtrip takes longer than 1 second.
yield from nc.flush(1)
except ErrTimeout:
print("Flush timeout")
yield from asyncio.sleep(1, loop=loop)
yield from nc.close()
示例4: test_reconnect_with_auth_token
# 需要导入模块: from nats.aio.client import Client [as 别名]
# 或者: from nats.aio.client.Client import request [as 别名]
def test_reconnect_with_auth_token(self):
nc = NATS()
disconnected_count = 0
reconnected_count = 0
closed_count = 0
err_count = 0
@asyncio.coroutine
def disconnected_cb():
nonlocal disconnected_count
disconnected_count += 1
@asyncio.coroutine
def reconnected_cb():
nonlocal reconnected_count
reconnected_count += 1
@asyncio.coroutine
def closed_cb():
nonlocal closed_count
closed_count += 1
counter = 0
@asyncio.coroutine
def worker_handler(msg):
nonlocal counter
counter += 1
if msg.reply != "":
yield from nc.publish(msg.reply, 'Reply:{}'.format(counter).encode())
options = {
'servers': [
"nats://[email protected]:4223",
"nats://[email protected]:4224",
],
'disconnected_cb': disconnected_cb,
'closed_cb': closed_cb,
'reconnected_cb': reconnected_cb,
'dont_randomize': True,
'io_loop': self.loop
}
yield from nc.connect(**options)
yield from nc.subscribe("test", cb=worker_handler)
self.assertIn('auth_required', nc._server_info)
self.assertTrue(nc.is_connected)
# Trigger a reconnnect
yield from self.loop.run_in_executor(None, self.server_pool[0].stop)
yield from asyncio.sleep(1, loop=self.loop)
yield from nc.subscribe("test", cb=worker_handler)
response = yield from nc.request("test", b'data', timeout=1)
self.assertEqual(b'Reply:1', response.data)
yield from nc.close()
self.assertTrue(nc.is_closed)
self.assertFalse(nc.is_connected)
self.assertEqual(1, closed_count)
self.assertEqual(2, disconnected_count)
self.assertEqual(1, reconnected_count)
示例5: test_auth_reconnect
# 需要导入模块: from nats.aio.client import Client [as 别名]
# 或者: from nats.aio.client.Client import request [as 别名]
def test_auth_reconnect(self):
nc = NATS()
disconnected_count = 0
reconnected_count = 0
closed_count = 0
err_count = 0
@asyncio.coroutine
def disconnected_cb():
nonlocal disconnected_count
disconnected_count += 1
@asyncio.coroutine
def reconnected_cb():
nonlocal reconnected_count
reconnected_count += 1
@asyncio.coroutine
def closed_cb():
nonlocal closed_count
closed_count += 1
@asyncio.coroutine
def err_cb(e):
nonlocal err_count
err_count += 1
counter = 0
@asyncio.coroutine
def worker_handler(msg):
nonlocal counter
counter += 1
if msg.reply != "":
yield from nc.publish(msg.reply, 'Reply:{}'.format(counter).encode())
options = {
'servers': [
"nats://foo:[email protected]:4223",
"nats://hoge:[email protected]:4224"
],
'io_loop': self.loop,
'disconnected_cb': disconnected_cb,
'closed_cb': closed_cb,
'reconnected_cb': reconnected_cb,
'error_cb': err_cb,
'dont_randomize': True,
}
yield from nc.connect(**options)
self.assertTrue(nc.is_connected)
yield from nc.subscribe("one", cb=worker_handler)
yield from nc.subscribe("two", cb=worker_handler)
yield from nc.subscribe("three", cb=worker_handler)
response = yield from nc.request("one", b'Help!', timeout=1)
self.assertEqual(b'Reply:1', response.data)
# Stop the first server and connect to another one asap.
yield from self.loop.run_in_executor(None, self.server_pool[0].stop)
# FIXME: Find better way to wait for the server to be stopped.
yield from asyncio.sleep(0.5, loop=self.loop)
response = yield from nc.request("three", b'Help!', timeout=1)
self.assertEqual('Reply:2'.encode(), response.data)
yield from asyncio.sleep(0.5, loop=self.loop)
yield from nc.close()
self.assertEqual(1, nc.stats['reconnects'])
self.assertEqual(1, closed_count)
self.assertEqual(2, disconnected_count)
self.assertEqual(1, reconnected_count)
self.assertEqual(1, err_count)
示例6: test_tls_reconnect
# 需要导入模块: from nats.aio.client import Client [as 别名]
# 或者: from nats.aio.client.Client import request [as 别名]
def test_tls_reconnect(self):
nc = NATS()
disconnected_count = 0
reconnected_count = 0
closed_count = 0
err_count = 0
@asyncio.coroutine
def disconnected_cb():
nonlocal disconnected_count
disconnected_count += 1
@asyncio.coroutine
def reconnected_cb():
nonlocal reconnected_count
reconnected_count += 1
@asyncio.coroutine
def closed_cb():
nonlocal closed_count
closed_count += 1
@asyncio.coroutine
def err_cb(e):
nonlocal err_count
err_count += 1
counter = 0
@asyncio.coroutine
def worker_handler(msg):
nonlocal counter
counter += 1
if msg.reply != "":
yield from nc.publish(msg.reply, 'Reply:{}'.format(counter).encode())
options = {
'servers': [
"nats://foo:[email protected]:4223",
"nats://hoge:[email protected]:4224"
],
'io_loop': self.loop,
'disconnected_cb': disconnected_cb,
'closed_cb': closed_cb,
'reconnected_cb': reconnected_cb,
'error_cb': err_cb,
'dont_randomize': True,
'tls': self.ssl_ctx
}
yield from nc.connect(**options)
self.assertTrue(nc.is_connected)
yield from nc.subscribe("example", cb=worker_handler)
response = yield from nc.request("example", b'Help!', timeout=1)
self.assertEqual(b'Reply:1', response.data)
# Trigger a reconnnect and should be fine
yield from self.loop.run_in_executor(None, self.server_pool[0].stop)
yield from asyncio.sleep(1, loop=self.loop)
yield from nc.subscribe("example", cb=worker_handler)
response = yield from nc.request("example", b'Help!', timeout=1)
self.assertEqual(b'Reply:2', response.data)
yield from nc.close()
self.assertTrue(nc.is_closed)
self.assertFalse(nc.is_connected)
self.assertEqual(1, nc.stats['reconnects'])
self.assertEqual(1, closed_count)
self.assertEqual(2, disconnected_count)
self.assertEqual(1, reconnected_count)
self.assertEqual(1, err_count)