本文整理汇总了Python中nats.aio.client.Client.flush方法的典型用法代码示例。如果您正苦于以下问题:Python Client.flush方法的具体用法?Python Client.flush怎么用?Python Client.flush使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类nats.aio.client.Client
的用法示例。
在下文中一共展示了Client.flush方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: go
# 需要导入模块: from nats.aio.client import Client [as 别名]
# 或者: from nats.aio.client.Client import flush [as 别名]
def go(loop):
nc = NATS()
try:
yield from nc.connect(io_loop=loop)
except:
pass
@asyncio.coroutine
def message_handler(msg):
print("[Received on '{}']: {}".format(msg.subject, msg.data.decode()))
try:
# Interested in receiving 2 messages from the 'discover' subject.
sid = yield from nc.subscribe("discover", "", message_handler)
yield from nc.auto_unsubscribe(sid, 2)
yield from nc.publish("discover", b'hello')
yield from nc.publish("discover", b'world')
# Following 2 messages won't be received.
yield from nc.publish("discover", b'again')
yield from nc.publish("discover", b'!!!!!')
except ErrConnectionClosed:
print("Connection closed prematurely")
@asyncio.coroutine
def request_handler(msg):
print("[Request on '{} {}']: {}".format(msg.subject, msg.reply, msg.data.decode()))
yield from nc.publish(msg.reply, b'OK')
if nc.is_connected:
# Subscription using a 'workers' queue so that only a single subscriber
# gets a request at a time.
yield from nc.subscribe("help", "workers", cb=request_handler)
try:
# Make a request expecting a single response within 500 ms,
# otherwise raising a timeout error.
msg = yield from nc.timed_request("help", b'help please', 0.500)
print("[Response]: {}".format(msg.data))
# Make a roundtrip to the server to ensure messages
# that sent messages have been processed already.
yield from nc.flush(0.500)
except ErrTimeout:
print("[Error] Timeout!")
# Wait a bit for message to be dispatched...
yield from asyncio.sleep(1, loop=loop)
# Detach from the server.
yield from nc.close()
if nc.last_error is not None:
print("Last Error: {}".format(nc.last_error))
if nc.is_closed:
print("Disconnected.")
示例2: test_pending_data_size_flush_on_close
# 需要导入模块: from nats.aio.client import Client [as 别名]
# 或者: from nats.aio.client.Client import flush [as 别名]
def test_pending_data_size_flush_on_close(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
options = {
'dont_randomize': True,
'io_loop': self.loop,
'disconnected_cb': disconnected_cb,
'closed_cb': closed_cb,
'reconnected_cb': reconnected_cb,
'reconnect_time_wait': 0.01
}
yield from nc.connect(**options)
total_received = 0
future = asyncio.Future(loop=self.loop)
@asyncio.coroutine
def receiver_cb(msg):
nonlocal total_received
total_received += 1
if total_received == 200:
future.set_result(True)
# Extra connection which should be receiving all the messages
nc2 = NATS()
yield from nc2.connect(**options)
yield from nc2.subscribe("example.*", cb=receiver_cb)
yield from nc2.flush()
for i in range(0, 200):
yield from nc.publish("example.{}".format(i), b'A' * 20)
# All pending messages should have been emitted to the server
# by the first connection at this point.
yield from nc.close()
# Wait for the server to flush all the messages back to the receiving client
yield from asyncio.wait_for(future, 1, loop=self.loop)
yield from nc2.close()
self.assertEqual(total_received, 200)
示例3: main
# 需要导入模块: from nats.aio.client import Client [as 别名]
# 或者: from nats.aio.client.Client import flush [as 别名]
def main(loop):
parser = argparse.ArgumentParser()
parser.add_argument('-n', '--count', default=DEFAULT_NUM_MSGS, type=int)
parser.add_argument('-s', '--size', default=DEFAULT_MSG_SIZE, type=int)
parser.add_argument('-S', '--subject', default='test')
parser.add_argument('-b', '--batch', default=DEFAULT_BATCH_SIZE, type=int)
parser.add_argument('--servers', default=[], action='append')
args = parser.parse_args()
data = []
for i in range(0, args.size):
s = "%01x" % randint(0, 15)
data.append(s.encode())
payload = b''.join(data)
servers = args.servers
if len(args.servers) < 1:
servers = ["nats://127.0.0.1:4222"]
opts = { "servers": servers, "io_loop": loop }
# 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()
# Start the benchmark
start = time.time()
to_send = args.count
print("Sending {0} messages of size {1} bytes on [{2}]".format(
args.count, args.size, args.subject))
while to_send > 0:
for i in range(0, args.batch):
to_send -= 1
yield from nc.publish(args.subject, payload)
if (to_send % HASH_MODULO) == 0:
sys.stdout.write("#")
sys.stdout.flush()
if to_send == 0:
break
# Minimal pause in between batches sent to server
yield from asyncio.sleep(0.00001, loop=loop)
# Additional roundtrip with server to try to ensure everything has been sent already.
try:
yield from nc.flush(DEFAULT_FLUSH_TIMEOUT)
except ErrTimeout:
print("Server flush timeout after {0}".format(DEFAULT_FLUSH_TIMEOUT))
elapsed = time.time() - start
mbytes = "%.1f" % (((args.size * args.count)/elapsed) / (1024*1024))
print("\nTest completed : {0} msgs/sec ({1}) MB/sec".format(
args.count/elapsed,
mbytes))
yield from nc.close()
示例4: test_flush
# 需要导入模块: from nats.aio.client import Client [as 别名]
# 或者: from nats.aio.client.Client import flush [as 别名]
def test_flush(self):
nc = NATS()
yield from nc.connect(io_loop=self.loop)
for i in range(0, 10):
yield from nc.publish("flush.%d" % i, b'AA')
yield from nc.flush()
self.assertEqual(10, nc.stats['out_msgs'])
self.assertEqual(20, nc.stats['out_bytes'])
yield from nc.close()
示例5: test_close
# 需要导入模块: from nats.aio.client import Client [as 别名]
# 或者: from nats.aio.client.Client import flush [as 别名]
def test_close(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():
nonlocal err_count
err_count += 1
options = {
'io_loop': self.loop,
'disconnected_cb': disconnected_cb,
'closed_cb': closed_cb,
'reconnected_cb': reconnected_cb,
'error_cb': err_cb,
}
yield from nc.connect(**options)
yield from nc.close()
with self.assertRaises(ErrConnectionClosed):
yield from nc.publish("foo", b'A')
with self.assertRaises(ErrConnectionClosed):
yield from nc.subscribe("bar", "workers")
with self.assertRaises(ErrConnectionClosed):
yield from nc.publish_request("bar", "inbox", b'B')
with self.assertRaises(ErrConnectionClosed):
yield from nc.flush()
self.assertEqual(1, closed_count)
self.assertEqual(1, disconnected_count)
self.assertEqual(0, reconnected_count)
self.assertEqual(0, err_count)
示例6: main
# 需要导入模块: from nats.aio.client import Client [as 别名]
# 或者: from nats.aio.client.Client import flush [as 别名]
def main(loop):
parser = argparse.ArgumentParser()
parser.add_argument('-n', '--count', default=DEFAULT_NUM_MSGS, 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, "io_loop": loop, "allow_reconnect": False }
# 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()
received = 0
start = None
@asyncio.coroutine
def handler(msg):
nonlocal received
nonlocal start
received += 1
# Measure time from when we get the first message.
if received == 1:
start = time.monotonic()
if (received % HASH_MODULO) == 0:
sys.stdout.write("*")
sys.stdout.flush()
yield from nc.subscribe(args.subject, cb=handler)
# Additional roundtrip with server to ensure everything has been
# processed by the server already.
yield from nc.flush()
print("Waiting for {} messages on [{}]...".format(args.count, args.subject))
try:
while received < args.count:
yield from asyncio.sleep(0.1, loop=loop)
except ErrTimeout:
print("Server flush timeout after {0}".format(DEFAULT_FLUSH_TIMEOUT))
elapsed = time.monotonic() - start
print("\nTest completed : {0} msgs/sec sent".format(args.count/elapsed))
print("Received {0} messages ({1} msgs/sec)".format(received, received/elapsed))
yield from nc.close()
示例7: run
# 需要导入模块: from nats.aio.client import Client [as 别名]
# 或者: from nats.aio.client.Client import flush [as 别名]
def run(loop):
parser = argparse.ArgumentParser()
# e.g. nats-pub hello -d "world" -s nats://127.0.0.1:4222 -s nats://127.0.0.1:4223
parser.add_argument('subject', default='hello', nargs='?')
parser.add_argument('-d', '--data', default="hello world")
parser.add_argument('-s', '--servers', default=[], action='append')
args = parser.parse_args()
nc = NATS()
@asyncio.coroutine
def closed_cb():
print("Connection to NATS is closed.")
loop.stop()
@asyncio.coroutine
def reconnected_cb():
print("Connected to NATS at {}...".format(nc.connected_url.netloc))
@asyncio.coroutine
def subscribe_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))
options = {
"io_loop": loop,
"closed_cb": closed_cb,
"reconnected_cb": reconnected_cb
}
try:
if len(args.servers) > 0:
options['servers'] = args.servers
yield from nc.connect(**options)
except Exception as e:
print(e)
show_usage_and_die()
print("Connected to NATS at {}...".format(nc.connected_url.netloc))
yield from nc.publish(args.subject, args.data.encode())
yield from nc.flush()
yield from nc.close()
示例8: run
# 需要导入模块: from nats.aio.client import Client [as 别名]
# 或者: from nats.aio.client.Client import flush [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()
示例9: test_publish
# 需要导入模块: from nats.aio.client import Client [as 别名]
# 或者: from nats.aio.client.Client import flush [as 别名]
def test_publish(self):
nc = NATS()
yield from nc.connect(io_loop=self.loop)
for i in range(0, 100):
yield from nc.publish("hello.%d" % i, b'A')
with self.assertRaises(ErrBadSubject):
yield from nc.publish("", b'')
yield from nc.flush()
yield from nc.close()
yield from asyncio.sleep(1, loop=self.loop)
self.assertEqual(100, nc.stats['out_msgs'])
self.assertEqual(100, nc.stats['out_bytes'])
endpoint = '127.0.0.1:{port}'.format(port=self.server_pool[0].http_port)
httpclient = http.client.HTTPConnection(endpoint, timeout=5)
httpclient.request('GET', '/varz')
response = httpclient.getresponse()
varz = json.loads((response.read()).decode())
self.assertEqual(100, varz['in_msgs'])
self.assertEqual(100, varz['in_bytes'])
示例10: Requester
# 需要导入模块: from nats.aio.client import Client [as 别名]
# 或者: from nats.aio.client.Client import flush [as 别名]
#.........这里部分代码省略.........
})
msgb = b''+bytes(msg_data, 'utf8')
split_mid = None
messages = []
if sys.getsizeof(msgb) > self.max_payload:
split_mid = str(uuid.uuid4())
messages = self._split_msg(split_mid, properties, my_args['body'])
else:
messages.append(msgb)
if not self.fire_and_forget:
if split_mid is not None and ('sessionID' not in properties or properties['sessionID'] is None or
not properties['sessionID']):
request_q += "_" + split_mid
self._init_split_msg_group(split_mid, request_q)
for msgb in messages:
try:
LOGGER.debug("natsd.Requester.call - publish splitted request " + str(typed_properties) +
" (size: " + str(sys.getsizeof(msgb)) + " bytes) on " + request_q)
next(self.nc.publish_request(request_q, self.responseQ, msgb))
except StopIteration as e:
pass
LOGGER.debug("natsd.Requester.call - waiting answer from " + self.responseQ)
else:
try:
LOGGER.debug("natsd.Requester.call - publish request " + str(typed_properties) + " on " + request_q)
next(self.nc.publish(request_q, b''+bytes(msg_data, 'utf8')))
except StopIteration as e:
pass
try:
next(self.nc.flush(1))
except StopIteration as e:
pass
start_time = timeit.default_timer()
if not self.fire_and_forget:
# Wait rpc_timeout sec before raising error
if self.rpc_timeout > 0:
exit_count = self.rpc_timeout * 100
else:
exit_count = 1
while self.response is None and exit_count > 0:
time.sleep(0.01)
if self.rpc_timeout > 0:
exit_count -= 1
if self.response is None:
if self.rpc_retry > 0:
if 'retry_count' not in my_args:
my_args['retry_count'] = 1
LOGGER.debug("natsd.Requester.call - Retry (" + str(my_args['retry_count']) + ")")
return self.call(my_args)
elif 'retry_count' in my_args and (self.rpc_retry - my_args['retry_count']) > 0:
LOGGER.warn("natsd.Requester.call - No response returned from request on " + request_q +
" queue after " + str(self.rpc_timeout) + '*' +
str(self.rpc_retry) + " sec ...")
self.trace = True
my_args['retry_count'] += 1
LOGGER.warn("natsd.Requester.call - Retry (" + str(my_args['retry_count']) + ")")
return self.call(my_args)
else:
self.rpc_retry_timeout_err_count += 1
if self.rpc_retry_timeout_err_count >= self.rpc_retry_timeout_err_count_max:
示例11: test_custom_flush_queue_reconnect
# 需要导入模块: from nats.aio.client import Client [as 别名]
# 或者: from nats.aio.client.Client import flush [as 别名]
def test_custom_flush_queue_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
options = {
'servers': [
"nats://foo:[email protected]:4223",
"nats://hoge:[email protected]:4224"
],
'dont_randomize': True,
'io_loop': self.loop,
'disconnected_cb': disconnected_cb,
'closed_cb': closed_cb,
'reconnected_cb': reconnected_cb,
'flusher_queue_size': 100,
'reconnect_time_wait': 0.01
}
yield from nc.connect(**options)
largest_pending_data_size = 0
post_flush_pending_data = None
done_once = False
@asyncio.coroutine
def cb(msg):
pass
yield from nc.subscribe("example.*", cb=cb)
for i in range(0, 500):
yield from nc.publish("example.{}".format(i), b'A' * 20)
if nc.pending_data_size > 0:
largest_pending_data_size = nc.pending_data_size
if nc.pending_data_size > 100:
# Stop the first server and connect to another one asap.
if not done_once:
yield from nc.flush(2)
post_flush_pending_data = nc.pending_data_size
yield from self.loop.run_in_executor(None, self.server_pool[0].stop)
done_once = True
self.assertTrue(largest_pending_data_size > 0)
self.assertTrue(post_flush_pending_data == 0)
# Confirm we have reconnected eventually
for i in range(0, 10):
yield from asyncio.sleep(0, loop=self.loop)
yield from asyncio.sleep(0.2, loop=self.loop)
yield from asyncio.sleep(0, loop=self.loop)
self.assertEqual(1, nc.stats['reconnects'])
try:
yield from nc.flush(2)
except ErrTimeout:
# If disconnect occurs during this flush, then we will have a timeout here
pass
finally:
yield from nc.close()
self.assertTrue(disconnected_count >= 1)
self.assertTrue(closed_count >= 1)
示例12: run
# 需要导入模块: from nats.aio.client import Client [as 别名]
# 或者: from nats.aio.client.Client import flush [as 别名]
def run(loop):
nc = NATS()
# Setup pool of servers from a NATS cluster.
options = {
"servers": [
"nats://user1:[email protected]:4222",
"nats://user2:[email protected]:4223",
"nats://user3:[email protected]:4224",
],
"io_loop": loop,
}
# Will try to connect to servers in order of configuration,
# by defaults it connect to one in the pool randomly.
options["dont_randomize"] = True
# Optionally set reconnect wait and max reconnect attempts.
# This example means 10 seconds total per backend.
options["max_reconnect_attempts"] = 5
options["reconnect_time_wait"] = 2
@asyncio.coroutine
def disconnected_cb():
print("Got disconnected!")
@asyncio.coroutine
def reconnected_cb():
# See who we are connected to on reconnect.
print("Got reconnected to {url}".format(url=nc.connected_url.netloc))
# Setup callbacks to be notified on disconnects and reconnects
options["disconnected_cb"] = disconnected_cb
options["reconnected_cb"] = reconnected_cb
@asyncio.coroutine
def error_cb(e):
print("There was an error: {}".format(e))
@asyncio.coroutine
def closed_cb():
print("Connection is closed")
# Setup callbacks to be notified when there is an error
# or connection is closed.
options["error_cb"] = error_cb
options["closed_cb"] = closed_cb
try:
yield from nc.connect(**options)
except ErrNoServers as e:
# Could not connect to any server in the cluster.
print(e)
return
if nc.is_connected:
yield from nc.subscribe("help.*")
max_messages = 1000000
start_time = datetime.now()
print("Sending {} messages to NATS...".format(max_messages))
for i in range(0, max_messages):
try:
yield from nc.publish("help.{}".format(i), b'A')
yield from nc.flush(0.500)
except ErrConnectionClosed as e:
print("Connection closed prematurely.")
break
except ErrTimeout as e:
print("Timeout occured when publishing msg i={}: {}".format(i, e))
end_time = datetime.now()
yield from nc.close()
duration = end_time - start_time
print("Duration: {}".format(duration))
try:
yield from nc.publish("help", b"hello world")
except ErrConnectionClosed:
print("Can't publish since no longer connected.")
err = nc.last_error
if err is not None:
print("Last Error: {}".format(err))