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


Python Client.subscribe_async方法代码示例

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


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

示例1: test_invalid_subscription_type

# 需要导入模块: from nats.aio.client import Client [as 别名]
# 或者: from nats.aio.client.Client import subscribe_async [as 别名]
  def test_invalid_subscription_type(self):
    nc = NATS()

    with self.assertRaises(NatsError):
      yield from nc.subscribe("hello", cb=None, future=None)

    with self.assertRaises(NatsError):
      yield from nc.subscribe_async("hello", cb=None)
开发者ID:habibutsu,项目名称:asyncio-nats,代码行数:10,代码来源:client_test.py

示例2: test_subscribe_async_without_coroutine_unsupported

# 需要导入模块: from nats.aio.client import Client [as 别名]
# 或者: from nats.aio.client.Client import subscribe_async [as 别名]
  def test_subscribe_async_without_coroutine_unsupported(self):
    nc = NATS()
    msgs = []

    def subscription_handler(msg):
      if msg.subject == "tests.1":
        time.sleep(0.5)
      if msg.subject == "tests.3":
        time.sleep(0.2)
      msgs.append(msg)

    yield from nc.connect(io_loop=self.loop)

    with self.assertRaises(NatsError):
      sid = yield from nc.subscribe_async("tests.>", cb=subscription_handler)
    yield from nc.close()
开发者ID:habibutsu,项目名称:asyncio-nats,代码行数:18,代码来源:client_test.py

示例3: test_async_await_subscribe_sync

# 需要导入模块: from nats.aio.client import Client [as 别名]
# 或者: from nats.aio.client.Client import subscribe_async [as 别名]
  def test_async_await_subscribe_sync(self):
    nc = NATS()
    msgs = []

    async def subscription_handler(msg):
      if msg.subject == "tests.1":
          await asyncio.sleep(0.5, loop=self.loop)
      if msg.subject == "tests.3":
          await asyncio.sleep(0.2, loop=self.loop)
      msgs.append(msg)

    yield from nc.connect(io_loop=self.loop)
    sid = yield from nc.subscribe_async("tests.>", cb=subscription_handler)

    for i in range(0, 5):
      yield from nc.publish("tests.{}".format(i), b'bar')

    # Wait a bit for messages to be received.
    yield from asyncio.sleep(1, loop=self.loop)
    self.assertEqual(5, len(msgs))
    self.assertEqual("tests.1", msgs[4].subject)
    self.assertEqual("tests.3", msgs[3].subject)
    yield from nc.close()
开发者ID:habibutsu,项目名称:asyncio-nats,代码行数:25,代码来源:client_async_await_test.py

示例4: main

# 需要导入模块: from nats.aio.client import Client [as 别名]
# 或者: from nats.aio.client.Client import subscribe_async [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('-t', '--subtype', default='sync')
    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()

    if args.subtype == 'sync':
        yield from nc.subscribe(args.subject, cb=handler)
    elif args.subtype == 'async':
        yield from nc.subscribe_async(args.subject, cb=handler)
    else:
        sys.stderr.write("ERROR: Unsupported type of subscription {0}".format(e))
        show_usage_and_die()

    print("Waiting for {} messages on [{}]...".format(args.count, args.subject))
    try:
        # Additional roundtrip with server to ensure everything has been
        # processed by the server already.
        yield from nc.flush()
    except ErrTimeout:
        print("Server flush timeout after {0}".format(DEFAULT_FLUSH_TIMEOUT))

    while received < args.count:
        yield from asyncio.sleep(0.1, loop=loop)

    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()
开发者ID:Gr1N,项目名称:asyncio-nats,代码行数:63,代码来源:sub_perf.py


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