本文整理汇总了Python中tornado.gen.moment方法的典型用法代码示例。如果您正苦于以下问题:Python gen.moment方法的具体用法?Python gen.moment怎么用?Python gen.moment使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类tornado.gen
的用法示例。
在下文中一共展示了gen.moment方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_moment
# 需要导入模块: from tornado import gen [as 别名]
# 或者: from tornado.gen import moment [as 别名]
def test_moment(self):
calls = []
@gen.coroutine
def f(name, yieldable):
for i in range(5):
calls.append(name)
yield yieldable
# First, confirm the behavior without moment: each coroutine
# monopolizes the event loop until it finishes.
immediate = Future()
immediate.set_result(None)
yield [f('a', immediate), f('b', immediate)]
self.assertEqual(''.join(calls), 'aaaaabbbbb')
# With moment, they take turns.
calls = []
yield [f('a', gen.moment), f('b', gen.moment)]
self.assertEqual(''.join(calls), 'ababababab')
self.finished = True
calls = []
yield [f('a', gen.moment), f('b', immediate)]
self.assertEqual(''.join(calls), 'abbbbbaaaa')
示例2: test_async_await
# 需要导入模块: from tornado import gen [as 别名]
# 或者: from tornado.gen import moment [as 别名]
def test_async_await(self):
@gen.coroutine
def f1():
yield gen.moment
raise gen.Return(42)
# This test verifies that an async function can await a
# yield-based gen.coroutine, and that a gen.coroutine
# (the test method itself) can yield an async function.
async def f2():
result = await f1()
return result
result = yield f2()
self.assertEqual(result, 42)
self.finished = True
示例3: test_async_await_mixed_multi_native_future
# 需要导入模块: from tornado import gen [as 别名]
# 或者: from tornado.gen import moment [as 别名]
def test_async_await_mixed_multi_native_future(self):
@gen.coroutine
def f1():
yield gen.moment
async def f2():
await f1()
return 42
@gen.coroutine
def f3():
yield gen.moment
raise gen.Return(43)
results = yield [f2(), f3()]
self.assertEqual(results, [42, 43])
self.finished = True
示例4: test_moment
# 需要导入模块: from tornado import gen [as 别名]
# 或者: from tornado.gen import moment [as 别名]
def test_moment(self):
calls = []
@gen.coroutine
def f(name, yieldable):
for i in range(5):
calls.append(name)
yield yieldable
# First, confirm the behavior without moment: each coroutine
# monopolizes the event loop until it finishes.
immediate = Future() # type: Future[None]
immediate.set_result(None)
yield [f("a", immediate), f("b", immediate)]
self.assertEqual("".join(calls), "aaaaabbbbb")
# With moment, they take turns.
calls = []
yield [f("a", gen.moment), f("b", gen.moment)]
self.assertEqual("".join(calls), "ababababab")
self.finished = True
calls = []
yield [f("a", gen.moment), f("b", immediate)]
self.assertEqual("".join(calls), "abbbbbaaaa")
示例5: test_asyncio_future_debug_info
# 需要导入模块: from tornado import gen [as 别名]
# 或者: from tornado.gen import moment [as 别名]
def test_asyncio_future_debug_info(self):
self.finished = True
# Enable debug mode
asyncio_loop = asyncio.get_event_loop()
self.addCleanup(asyncio_loop.set_debug, asyncio_loop.get_debug())
asyncio_loop.set_debug(True)
def f():
yield gen.moment
coro = gen.coroutine(f)()
self.assertIsInstance(coro, asyncio.Future)
# We expect the coroutine repr() to show the place where
# it was instantiated
expected = "created at %s:%d" % (__file__, f.__code__.co_firstlineno + 3)
actual = repr(coro)
self.assertIn(expected, actual)
示例6: test_async_await
# 需要导入模块: from tornado import gen [as 别名]
# 或者: from tornado.gen import moment [as 别名]
def test_async_await(self):
@gen.coroutine
def f1():
yield gen.moment
raise gen.Return(42)
# This test verifies that an async function can await a
# yield-based gen.coroutine, and that a gen.coroutine
# (the test method itself) can yield an async function.
namespace = exec_test(globals(), locals(), """
async def f2():
result = await f1()
return result
""")
result = yield namespace['f2']()
self.assertEqual(result, 42)
self.finished = True
示例7: test_async_await_mixed_multi_native_future
# 需要导入模块: from tornado import gen [as 别名]
# 或者: from tornado.gen import moment [as 别名]
def test_async_await_mixed_multi_native_future(self):
@gen.coroutine
def f1():
yield gen.moment
namespace = exec_test(globals(), locals(), """
async def f2():
await f1()
return 42
""")
@gen.coroutine
def f3():
yield gen.moment
raise gen.Return(43)
results = yield [namespace['f2'](), f3()]
self.assertEqual(results, [42, 43])
self.finished = True
示例8: test_asyncio_future_debug_info
# 需要导入模块: from tornado import gen [as 别名]
# 或者: from tornado.gen import moment [as 别名]
def test_asyncio_future_debug_info(self):
self.finished = True
# Enable debug mode
asyncio_loop = asyncio.get_event_loop()
self.addCleanup(asyncio_loop.set_debug, asyncio_loop.get_debug())
asyncio_loop.set_debug(True)
def f():
yield gen.moment
coro = gen.coroutine(f)()
self.assertIsInstance(coro, asyncio.Future)
# We expect the coroutine repr() to show the place where
# it was instantiated
expected = ("created at %s:%d"
% (__file__, f.__code__.co_firstlineno + 3))
actual = repr(coro)
self.assertIn(expected, actual)
示例9: test_future_traceback
# 需要导入模块: from tornado import gen [as 别名]
# 或者: from tornado.gen import moment [as 别名]
def test_future_traceback(self):
@gen.coroutine
def f():
yield gen.moment
try:
1 / 0
except ZeroDivisionError:
self.expected_frame = traceback.extract_tb(
sys.exc_info()[2], limit=1)[0]
raise
try:
yield f()
self.fail("didn't get expected exception")
except ZeroDivisionError:
tb = traceback.extract_tb(sys.exc_info()[2])
self.assertIn(self.expected_frame, tb)
示例10: _server_request_loop
# 需要导入模块: from tornado import gen [as 别名]
# 或者: from tornado.gen import moment [as 别名]
def _server_request_loop(self, delegate):
try:
while True:
conn = HTTP1Connection(self.stream, False,
self.params, self.context)
request_delegate = delegate.start_request(self, conn)
try:
ret = yield conn.read_response(request_delegate)
except (iostream.StreamClosedError,
iostream.UnsatisfiableReadError):
return
except _QuietException:
# This exception was already logged.
conn.close()
return
except Exception:
gen_log.error("Uncaught exception", exc_info=True)
conn.close()
return
if not ret:
return
yield gen.moment
finally:
delegate.on_close(self)
示例11: test_handle_stream_coroutine_logging
# 需要导入模块: from tornado import gen [as 别名]
# 或者: from tornado.gen import moment [as 别名]
def test_handle_stream_coroutine_logging(self):
# handle_stream may be a coroutine and any exception in its
# Future will be logged.
class TestServer(TCPServer):
@gen.coroutine
def handle_stream(self, stream, address):
yield gen.moment
stream.close()
1 / 0
server = client = None
try:
sock, port = bind_unused_port()
with NullContext():
server = TestServer()
server.add_socket(sock)
client = IOStream(socket.socket())
with ExpectLog(app_log, "Exception in callback"):
yield client.connect(('localhost', port))
yield client.read_until_close()
yield gen.moment
finally:
if server is not None:
server.stop()
if client is not None:
client.close()
示例12: async_exception
# 需要导入模块: from tornado import gen [as 别名]
# 或者: from tornado.gen import moment [as 别名]
def async_exception(self, e):
yield gen.moment
raise e
示例13: test_nonblocking_put_with_getters
# 需要导入模块: from tornado import gen [as 别名]
# 或者: from tornado.gen import moment [as 别名]
def test_nonblocking_put_with_getters(self):
q = queues.Queue()
get0 = q.get()
get1 = q.get()
q.put_nowait(0)
# put_nowait does *not* immediately unblock getters.
yield gen.moment
self.assertEqual(0, (yield get0))
q.put_nowait(1)
yield gen.moment
self.assertEqual(1, (yield get1))
示例14: test_task_done_delay
# 需要导入模块: from tornado import gen [as 别名]
# 或者: from tornado.gen import moment [as 别名]
def test_task_done_delay(self):
# Verify it is task_done(), not get(), that unblocks join().
q = self.queue_class()
q.put_nowait(0)
join = q.join()
self.assertFalse(join.done())
yield q.get()
self.assertFalse(join.done())
yield gen.moment
self.assertFalse(join.done())
q.task_done()
self.assertTrue(join.done())
示例15: test_handle_stream_coroutine_logging
# 需要导入模块: from tornado import gen [as 别名]
# 或者: from tornado.gen import moment [as 别名]
def test_handle_stream_coroutine_logging(self):
# handle_stream may be a coroutine and any exception in its
# Future will be logged.
class TestServer(TCPServer):
@gen.coroutine
def handle_stream(self, stream, address):
yield stream.read_bytes(len(b"hello"))
stream.close()
1 / 0
server = client = None
try:
sock, port = bind_unused_port()
server = TestServer()
server.add_socket(sock)
client = IOStream(socket.socket())
with ExpectLog(app_log, "Exception in callback"):
yield client.connect(("localhost", port))
yield client.write(b"hello")
yield client.read_until_close()
yield gen.moment
finally:
if server is not None:
server.stop()
if client is not None:
client.close()