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


Python gen.Task方法代码示例

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


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

示例1: test_stack_context_leak_exception

# 需要导入模块: from tornado import gen [as 别名]
# 或者: from tornado.gen import Task [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 Task [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_async_await_mixed_multi_native_yieldpoint

# 需要导入模块: from tornado import gen [as 别名]
# 或者: from tornado.gen import Task [as 别名]
def test_async_await_mixed_multi_native_yieldpoint(self):
        namespace = exec_test(globals(), locals(), """
        async def f1():
            await gen.Task(self.io_loop.add_callback)
            return 42
        """)

        @gen.coroutine
        def f2():
            yield gen.Task(self.io_loop.add_callback)
            raise gen.Return(43)

        f2(callback=(yield gen.Callback('cb')))
        results = yield [namespace['f1'](), gen.Wait('cb')]
        self.assertEqual(results, [42, 43])
        self.finished = True 
开发者ID:tao12345666333,项目名称:tornado-zh,代码行数:18,代码来源:gen_test.py

示例4: test_swallow_context_exception

# 需要导入模块: from tornado import gen [as 别名]
# 或者: from tornado.gen import Task [as 别名]
def test_swallow_context_exception(self):
        # Test exception handling: exceptions thrown into the stack context
        # can be caught and ignored.
        @gen.coroutine
        def f2():
            (yield gen.Callback(1))()
            yield gen.Wait(1)
            self.io_loop.add_callback(lambda: 1 / 0)
            try:
                yield gen.Task(self.io_loop.add_timeout,
                               self.io_loop.time() + 10)
            except ZeroDivisionError:
                raise gen.Return(42)

        result = yield f2()
        self.assertEqual(result, 42)
        self.finished = True 
开发者ID:tao12345666333,项目名称:tornado-zh,代码行数:19,代码来源:gen_test.py

示例5: test_timeout

# 需要导入模块: from tornado import gen [as 别名]
# 或者: from tornado.gen import Task [as 别名]
def test_timeout(self):
        # Set a short timeout and exceed it.
        @gen_test(timeout=0.1)
        def test(self):
            yield gen.Task(self.io_loop.add_timeout, self.io_loop.time() + 1)

        # This can't use assertRaises because we need to inspect the
        # exc_info triple (and not just the exception object)
        try:
            test(self)
            self.fail("did not get expected exception")
        except ioloop.TimeoutError:
            # The stack trace should blame the add_timeout line, not just
            # unrelated IOLoop/testing internals.
            self.assertIn(
                "gen.Task(self.io_loop.add_timeout, self.io_loop.time() + 1)",
                traceback.format_exc())

        self.finished = True 
开发者ID:tao12345666333,项目名称:tornado-zh,代码行数:21,代码来源:testing_test.py

示例6: test_run_with_stack_context

# 需要导入模块: from tornado import gen [as 别名]
# 或者: from tornado.gen import Task [as 别名]
def test_run_with_stack_context(self):
        @gen.coroutine
        def f1():
            self.assertEqual(self.active_contexts, ['c1'])
            yield run_with_stack_context(
                StackContext(functools.partial(self.context, 'c2')),
                f2)
            self.assertEqual(self.active_contexts, ['c1'])

        @gen.coroutine
        def f2():
            self.assertEqual(self.active_contexts, ['c1', 'c2'])
            yield gen.Task(self.io_loop.add_callback)
            self.assertEqual(self.active_contexts, ['c1', 'c2'])

        self.assertEqual(self.active_contexts, [])
        yield run_with_stack_context(
            StackContext(functools.partial(self.context, 'c1')),
            f1)
        self.assertEqual(self.active_contexts, []) 
开发者ID:tao12345666333,项目名称:tornado-zh,代码行数:22,代码来源:stack_context_test.py

示例7: test_future_traceback

# 需要导入模块: from tornado import gen [as 别名]
# 或者: from tornado.gen import Task [as 别名]
def test_future_traceback(self):
        @return_future
        @gen.engine
        def f(callback):
            yield gen.Task(self.io_loop.add_callback)
            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)

# The following series of classes demonstrate and test various styles
# of use, with and without generators and futures. 
开发者ID:tao12345666333,项目名称:tornado-zh,代码行数:22,代码来源:concurrent_test.py

示例8: test_body_size_override_reset

# 需要导入模块: from tornado import gen [as 别名]
# 或者: from tornado.gen import Task [as 别名]
def test_body_size_override_reset(self):
        # The max_body_size override is reset between requests.
        stream = IOStream(socket.socket())
        try:
            yield stream.connect(('127.0.0.1', self.get_http_port()))
            # Use a raw stream so we can make sure it's all on one connection.
            stream.write(b'PUT /streaming?expected_size=10240 HTTP/1.1\r\n'
                         b'Content-Length: 10240\r\n\r\n')
            stream.write(b'a' * 10240)
            headers, response = yield gen.Task(read_stream_body, stream)
            self.assertEqual(response, b'10240')
            # Without the ?expected_size parameter, we get the old default value
            stream.write(b'PUT /streaming HTTP/1.1\r\n'
                         b'Content-Length: 10240\r\n\r\n')
            with ExpectLog(gen_log, '.*Content-Length too long'):
                data = yield stream.read_until_close()
            self.assertEqual(data, b'')
        finally:
            stream.close() 
开发者ID:tao12345666333,项目名称:tornado-zh,代码行数:21,代码来源:httpserver_test.py

示例9: _listen

# 需要导入模块: from tornado import gen [as 别名]
# 或者: from tornado.gen import Task [as 别名]
def _listen(self, room):
        self.redis_chan = ChatLogger.CHANNEL.format(channel=room)
        yield gen.Task(self.r.subscribe, self.redis_chan)
        self.r.listen(self._on_update) 
开发者ID:tuna,项目名称:fishroom,代码行数:6,代码来源:handlers.py

示例10: get

# 需要导入模块: from tornado import gen [as 别名]
# 或者: from tornado.gen import Task [as 别名]
def get(self):
        token_id = self.auth()
        if token_id is None:
            self.finish("Invalid Token")
            return

        room = self.get_argument("room", None)
        if room not in config["bindings"] or \
                room in config.get("private_rooms", []):
            self.set_status(404)
            self.finish("Room not found")
            return

        queue = APIClientManager.queue_key.format(token_id=token_id)
        l = yield gen.Task(r.llen, queue)
        msgs = []
        if l > 0:
            msgs = yield gen.Task(r.lrange, queue, 0, -1)
            pr.delete(queue)
            msgs = [json.loads(m) for m in msgs]
        else:
            ret = yield gen.Task(r.blpop, queue, timeout=10)
            if queue in ret:
                msgs = [json.loads(ret[queue])]

        if room:
            msgs = [m for m in msgs if m['room'] == room]

        self.write_json(messages=msgs)
        self.finish() 
开发者ID:tuna,项目名称:fishroom,代码行数:32,代码来源:handlers.py

示例11: do_task

# 需要导入模块: from tornado import gen [as 别名]
# 或者: from tornado.gen import Task [as 别名]
def do_task(self, task):
        self.running_task += 1
        result = yield gen.Task(self.fetcher.fetch, task)
        type, task, response = result.args
        self.processor.on_task(task, response)
        # do with message
        while not self.processor.inqueue.empty():
            _task, _response = self.processor.inqueue.get()
            self.processor.on_task(_task, _response)
        # do with results
        while not self.processor.result_queue.empty():
            _task, _result = self.processor.result_queue.get()
            if self.result_worker:
                self.result_worker.on_result(_task, _result)
        self.running_task -= 1 
开发者ID:binux,项目名称:pyspider,代码行数:17,代码来源:scheduler.py

示例12: e1

# 需要导入模块: from tornado import gen [as 别名]
# 或者: from tornado.gen import Task [as 别名]
def e1():
    for i in range(10):
        yield gen.Task(e2) 
开发者ID:tao12345666333,项目名称:tornado-zh,代码行数:5,代码来源:gen_benchmark.py

示例13: resolve

# 需要导入模块: from tornado import gen [as 别名]
# 或者: from tornado.gen import Task [as 别名]
def resolve(self, host, port, family=0):
        # getHostByName doesn't accept IP addresses, so if the input
        # looks like an IP address just return it immediately.
        if twisted.internet.abstract.isIPAddress(host):
            resolved = host
            resolved_family = socket.AF_INET
        elif twisted.internet.abstract.isIPv6Address(host):
            resolved = host
            resolved_family = socket.AF_INET6
        else:
            deferred = self.resolver.getHostByName(utf8(host))
            resolved = yield gen.Task(deferred.addBoth)
            if isinstance(resolved, failure.Failure):
                resolved.raiseException()
            elif twisted.internet.abstract.isIPAddress(resolved):
                resolved_family = socket.AF_INET
            elif twisted.internet.abstract.isIPv6Address(resolved):
                resolved_family = socket.AF_INET6
            else:
                resolved_family = socket.AF_UNSPEC
        if family != socket.AF_UNSPEC and family != resolved_family:
            raise Exception('Requested socket family %d but got %d' %
                            (family, resolved_family))
        result = [
            (resolved_family, (resolved, port)),
        ]
        raise gen.Return(result) 
开发者ID:tao12345666333,项目名称:tornado-zh,代码行数:29,代码来源:twisted.py

示例14: test_exception_in_task_phase1

# 需要导入模块: from tornado import gen [as 别名]
# 或者: from tornado.gen import Task [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

示例15: test_exception_in_task_phase2

# 需要导入模块: from tornado import gen [as 别名]
# 或者: from tornado.gen import Task [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


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