當前位置: 首頁>>代碼示例>>Python>>正文


Python gen.engine方法代碼示例

本文整理匯總了Python中tornado.gen.engine方法的典型用法代碼示例。如果您正苦於以下問題:Python gen.engine方法的具體用法?Python gen.engine怎麽用?Python gen.engine使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在tornado.gen的用法示例。


在下文中一共展示了gen.engine方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: test_stack_context_leak_exception

# 需要導入模塊: from tornado import gen [as 別名]
# 或者: from tornado.gen import engine [as 別名]
def test_stack_context_leak_exception(self):
        # same as previous, but with a function that exits with an exception
        @gen.engine
        def inner(callback):
            yield gen.Task(self.io_loop.add_callback)
            1 / 0

        @gen.engine
        def outer():
            for i in range(10):
                try:
                    yield gen.Task(inner)
                except ZeroDivisionError:
                    pass
            stack_increase = len(stack_context._state.contexts) - initial_stack_depth
            self.assertTrue(stack_increase <= 2)
            self.stop()
        initial_stack_depth = len(stack_context._state.contexts)
        self.run_gen(outer) 
開發者ID:tao12345666333,項目名稱:tornado-zh,代碼行數:21,代碼來源:gen_test.py

示例2: test_task_refcounting

# 需要導入模塊: from tornado import gen [as 別名]
# 或者: from tornado.gen import engine [as 別名]
def test_task_refcounting(self):
        # On CPython, tasks and their arguments should be released immediately
        # without waiting for garbage collection.
        @gen.engine
        def f():
            class Foo(object):
                pass
            arg = Foo()
            self.arg_ref = weakref.ref(arg)
            task = gen.Task(self.io_loop.add_callback, arg=arg)
            self.task_ref = weakref.ref(task)
            yield task
            self.stop()

        self.run_gen(f)
        self.assertIs(self.arg_ref(), None)
        self.assertIs(self.task_ref(), None) 
開發者ID:tao12345666333,項目名稱:tornado-zh,代碼行數:19,代碼來源:gen_test.py

示例3: test_yield_in_with

# 需要導入模塊: from tornado import gen [as 別名]
# 或者: from tornado.gen import engine [as 別名]
def test_yield_in_with(self):
        @gen.engine
        def f():
            self.callback = yield gen.Callback('a')
            with StackContext(functools.partial(self.context, 'c1')):
                # This yield is a problem: the generator will be suspended
                # and the StackContext's __exit__ is not called yet, so
                # the context will be left on _state.contexts for anything
                # that runs before the yield resolves.
                yield gen.Wait('a')

        with self.assertRaises(StackContextInconsistentError):
            f()
            self.wait()
        # Cleanup: to avoid GC warnings (which for some reason only seem
        # to show up on py33-asyncio), invoke the callback (which will do
        # nothing since the gen.Runner is already finished) and delete it.
        self.callback()
        del self.callback 
開發者ID:tao12345666333,項目名稱:tornado-zh,代碼行數:21,代碼來源:stack_context_test.py

示例4: main

# 需要導入模塊: from tornado import gen [as 別名]
# 或者: from tornado.gen import engine [as 別名]
def main():
    parse_command_line()
    t = Timer(e1)
    results = t.timeit(options.num) / options.num
    print('engine: %0.3f ms per iteration' % (results * 1000))
    t = Timer(c1)
    results = t.timeit(options.num) / options.num
    print('coroutine: %0.3f ms per iteration' % (results * 1000)) 
開發者ID:tao12345666333,項目名稱:tornado-zh,代碼行數:10,代碼來源:gen_benchmark.py

示例5: test_no_yield

# 需要導入模塊: from tornado import gen [as 別名]
# 或者: from tornado.gen import engine [as 別名]
def test_no_yield(self):
        @gen.engine
        def f():
            self.stop()
        self.run_gen(f) 
開發者ID:tao12345666333,項目名稱:tornado-zh,代碼行數:7,代碼來源:gen_test.py

示例6: test_inline_cb

# 需要導入模塊: from tornado import gen [as 別名]
# 或者: from tornado.gen import engine [as 別名]
def test_inline_cb(self):
        @gen.engine
        def f():
            (yield gen.Callback("k1"))()
            res = yield gen.Wait("k1")
            self.assertTrue(res is None)
            self.stop()
        self.run_gen(f) 
開發者ID:tao12345666333,項目名稱:tornado-zh,代碼行數:10,代碼來源:gen_test.py

示例7: test_ioloop_cb

# 需要導入模塊: from tornado import gen [as 別名]
# 或者: from tornado.gen import engine [as 別名]
def test_ioloop_cb(self):
        @gen.engine
        def f():
            self.io_loop.add_callback((yield gen.Callback("k1")))
            yield gen.Wait("k1")
            self.stop()
        self.run_gen(f) 
開發者ID:tao12345666333,項目名稱:tornado-zh,代碼行數:9,代碼來源:gen_test.py

示例8: test_exception_phase1

# 需要導入模塊: from tornado import gen [as 別名]
# 或者: from tornado.gen import engine [as 別名]
def test_exception_phase1(self):
        @gen.engine
        def f():
            1 / 0
        self.assertRaises(ZeroDivisionError, self.run_gen, f) 
開發者ID:tao12345666333,項目名稱:tornado-zh,代碼行數:7,代碼來源:gen_test.py

示例9: test_exception_in_task_phase1

# 需要導入模塊: from tornado import gen [as 別名]
# 或者: from tornado.gen import engine [as 別名]
def test_exception_in_task_phase1(self):
        def fail_task(callback):
            1 / 0

        @gen.engine
        def f():
            try:
                yield gen.Task(fail_task)
                raise Exception("did not get expected exception")
            except ZeroDivisionError:
                self.stop()
        self.run_gen(f) 
開發者ID:tao12345666333,項目名稱:tornado-zh,代碼行數:14,代碼來源:gen_test.py

示例10: test_exception_in_task_phase2

# 需要導入模塊: from tornado import gen [as 別名]
# 或者: from tornado.gen import engine [as 別名]
def test_exception_in_task_phase2(self):
        # This is the case that requires the use of stack_context in gen.engine
        def fail_task(callback):
            self.io_loop.add_callback(lambda: 1 / 0)

        @gen.engine
        def f():
            try:
                yield gen.Task(fail_task)
                raise Exception("did not get expected exception")
            except ZeroDivisionError:
                self.stop()
        self.run_gen(f) 
開發者ID:tao12345666333,項目名稱:tornado-zh,代碼行數:15,代碼來源:gen_test.py

示例11: test_with_arg

# 需要導入模塊: from tornado import gen [as 別名]
# 或者: from tornado.gen import engine [as 別名]
def test_with_arg(self):
        @gen.engine
        def f():
            (yield gen.Callback("k1"))(42)
            res = yield gen.Wait("k1")
            self.assertEqual(42, res)
            self.stop()
        self.run_gen(f) 
開發者ID:tao12345666333,項目名稱:tornado-zh,代碼行數:10,代碼來源:gen_test.py

示例12: test_with_arg_tuple

# 需要導入模塊: from tornado import gen [as 別名]
# 或者: from tornado.gen import engine [as 別名]
def test_with_arg_tuple(self):
        @gen.engine
        def f():
            (yield gen.Callback((1, 2)))((3, 4))
            res = yield gen.Wait((1, 2))
            self.assertEqual((3, 4), res)
            self.stop()
        self.run_gen(f) 
開發者ID:tao12345666333,項目名稱:tornado-zh,代碼行數:10,代碼來源:gen_test.py

示例13: test_key_reuse

# 需要導入模塊: from tornado import gen [as 別名]
# 或者: from tornado.gen import engine [as 別名]
def test_key_reuse(self):
        @gen.engine
        def f():
            yield gen.Callback("k1")
            yield gen.Callback("k1")
            self.stop()
        self.assertRaises(gen.KeyReuseError, self.run_gen, f) 
開發者ID:tao12345666333,項目名稱:tornado-zh,代碼行數:9,代碼來源:gen_test.py

示例14: test_key_mismatch

# 需要導入模塊: from tornado import gen [as 別名]
# 或者: from tornado.gen import engine [as 別名]
def test_key_mismatch(self):
        @gen.engine
        def f():
            yield gen.Callback("k1")
            yield gen.Wait("k2")
            self.stop()
        self.assertRaises(gen.UnknownKeyError, self.run_gen, f) 
開發者ID:tao12345666333,項目名稱:tornado-zh,代碼行數:9,代碼來源:gen_test.py

示例15: test_key_mismatch_tuple

# 需要導入模塊: from tornado import gen [as 別名]
# 或者: from tornado.gen import engine [as 別名]
def test_key_mismatch_tuple(self):
        @gen.engine
        def f():
            yield gen.Callback((1, 2))
            yield gen.Wait((2, 3))
            self.stop()
        self.assertRaises(gen.UnknownKeyError, self.run_gen, f) 
開發者ID:tao12345666333,項目名稱:tornado-zh,代碼行數:9,代碼來源:gen_test.py


注:本文中的tornado.gen.engine方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。