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


Python gen.Return方法代码示例

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


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

示例1: get_links_from_url

# 需要导入模块: from tornado import gen [as 别名]
# 或者: from tornado.gen import Return [as 别名]
def get_links_from_url(url):
    """Download the page at `url` and parse it for links.

    Returned links have had the fragment after `#` removed, and have been made
    absolute so, e.g. the URL 'gen.html#tornado.gen.coroutine' becomes
    'http://www.tornadoweb.org/en/stable/gen.html'.
    """
    try:
        response = yield httpclient.AsyncHTTPClient().fetch(url)
        print('fetched %s' % url)

        html = response.body if isinstance(response.body, str) \
            else response.body.decode()
        urls = [urljoin(url, remove_fragment(new_url))
                for new_url in get_links(html)]
    except Exception as e:
        print('Exception: %s %s' % (e, url))
        raise gen.Return([])

    raise gen.Return(urls) 
开发者ID:tao12345666333,项目名称:tornado-zh,代码行数:22,代码来源:webspider.py

示例2: connect

# 需要导入模块: from tornado import gen [as 别名]
# 或者: from tornado.gen import Return [as 别名]
def connect(self, host, port, af=socket.AF_UNSPEC, ssl_options=None,
                max_buffer_size=None):
        """Connect to the given host and port.

        Asynchronously returns an `.IOStream` (or `.SSLIOStream` if
        ``ssl_options`` is not None).
        """
        addrinfo = yield self.resolver.resolve(host, port, af)
        connector = _Connector(
            addrinfo, self.io_loop,
            functools.partial(self._create_stream, max_buffer_size))
        af, addr, stream = yield connector.start()
        # TODO: For better performance we could cache the (af, addr)
        # information here and re-use it on subsequent connections to
        # the same host. (http://tools.ietf.org/html/rfc6555#section-4.2)
        if ssl_options is not None:
            stream = yield stream.start_tls(False, ssl_options=ssl_options,
                                            server_hostname=host)
        raise gen.Return(stream) 
开发者ID:tao12345666333,项目名称:tornado-zh,代码行数:21,代码来源:tcpclient.py

示例3: test_async_await_mixed_multi_native_yieldpoint

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

# 需要导入模块: from tornado import gen [as 别名]
# 或者: from tornado.gen import Return [as 别名]
def test_swallow_yieldpoint_exception(self):
        # Test exception handling: a coroutine can catch an exception
        # raised by a yield point and not raise a different one.
        @gen.coroutine
        def f1():
            1 / 0

        @gen.coroutine
        def f2():
            try:
                yield f1()
            except ZeroDivisionError:
                raise gen.Return(42)

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

示例6: get_links_from_url

# 需要导入模块: from tornado import gen [as 别名]
# 或者: from tornado.gen import Return [as 别名]
def get_links_from_url(url):
    """Download the page at `url` and parse it for links.

    Returned links have had the fragment after `#` removed, and have been made
    absolute so, e.g. the URL 'gen.html#tornado.gen.coroutine' becomes
    'http://www.tornadoweb.org/en/stable/gen.html'.
    """
    try:
        response = yield httpclient.AsyncHTTPClient().fetch(url)#获取到
        print('fetched %s' % url)


        html = response.body if isinstance(response.body, str) \
            else response.body.decode()
        urls = [urljoin(url, remove_fragment(new_url))
                for new_url in get_links(html)]
    except Exception as e:
        print('Exception: %s %s' % (e, url))
        raise gen.Return([])

    raise gen.Return(urls) 
开发者ID:makelove,项目名称:Python_Master_Courses,代码行数:23,代码来源:tornado-crawler-demo1.py

示例7: open

# 需要导入模块: from tornado import gen [as 别名]
# 或者: from tornado.gen import Return [as 别名]
def open(self, timeout=None):
        logger.debug('socket connecting')
        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0)
        self.stream = iostream.IOStream(sock)

        try:
            connect = self.stream.connect((self.host, self.port))
            if timeout is not None:
                yield self.with_timeout(timeout, connect)
            else:
                yield connect
        except (socket.error, IOError, ioloop.TimeoutError) as e:
            message = 'could not connect to {}:{} ({})'.format(self.host, self.port, e)
            raise TTransportException(
                type=TTransportException.NOT_OPEN,
                message=message)

        raise gen.Return(self) 
开发者ID:XiaoMi,项目名称:galaxy-sdk-python,代码行数:20,代码来源:TTornado.py

示例8: test_async_await_mixed_multi_native_future

# 需要导入模块: from tornado import gen [as 别名]
# 或者: from tornado.gen import Return [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 
开发者ID:opendevops-cn,项目名称:opendevops,代码行数:19,代码来源:gen_test.py

示例9: _do_all_execute_forasync

# 需要导入模块: from tornado import gen [as 别名]
# 或者: from tornado.gen import Return [as 别名]
def _do_all_execute_forasync(self, handler, clear, method_name, **kwargs):

        for c_module in self.common_modules:
            result = self._execute_module(handler, clear, c_module, getattr(c_module, method_name), **kwargs)
            if is_future(result):
                result = yield result
            if result:
                raise gen.Return(1)

        for name, r_module in self.route_modules.items():
            for md in r_module:
                result = self._execute_module(handler, clear, md, getattr(md, method_name), name, **kwargs)
                if is_future(result):
                    result = yield result
                if result:
                    raise gen.Return(1) 
开发者ID:mqingyn,项目名称:torngas,代码行数:18,代码来源:httpmodule.py

示例10: _call_subprocess

# 需要导入模块: from tornado import gen [as 别名]
# 或者: from tornado.gen import Return [as 别名]
def _call_subprocess(self, function_to_evaluate, arguments):
        restricted_tabpy = RestrictedTabPy(
            self.protocol, self.port, self.logger, self.eval_timeout
        )
        # Exec does not run the function, so it does not block.
        exec(function_to_evaluate, globals())

        # 'noqa' comments below tell flake8 to ignore undefined _user_script
        # name - the name is actually defined with user script being wrapped
        # in _user_script function (constructed as a striong) and then executed
        # with exec() call above.
        if arguments is None:
            future = self.executor.submit(_user_script,  # noqa: F821
                                          restricted_tabpy)
        else:
            future = self.executor.submit(_user_script,  # noqa: F821
                                          restricted_tabpy, **arguments)

        ret = yield gen.with_timeout(timedelta(seconds=self.eval_timeout), future)
        raise gen.Return(ret) 
开发者ID:tableau,项目名称:TabPy,代码行数:22,代码来源:evaluation_plane_handler.py

示例11: resolve

# 需要导入模块: from tornado import gen [as 别名]
# 或者: from tornado.gen import Return [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.addCallback)
            if 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:viewfinderco,项目名称:viewfinder,代码行数:27,代码来源:twisted.py

示例12: send_task

# 需要导入模块: from tornado import gen [as 别名]
# 或者: from tornado.gen import Return [as 别名]
def send_task(self, message):
        request_context = {}

        @gen.coroutine
        def before_handler():
            self.request_handler.before_request(message, request_context)

        @gen.coroutine
        def after_handler():
            self.request_handler.after_request(message, request_context)

        yield before_handler()
        yield after_handler()

        raise gen.Return('%s::response' % message) 
开发者ID:opentracing,项目名称:opentracing-python,代码行数:17,代码来源:test_tornado.py

示例13: parent_task

# 需要导入模块: from tornado import gen [as 别名]
# 或者: from tornado.gen import Return [as 别名]
def parent_task(self, message):
        with self.tracer.start_active_span('parent'):
            res = yield self.child_task(message)

        raise gen.Return(res) 
开发者ID:opentracing,项目名称:opentracing-python,代码行数:7,代码来源:test_tornado.py

示例14: child_task

# 需要导入模块: from tornado import gen [as 别名]
# 或者: from tornado.gen import Return [as 别名]
def child_task(self, message):
        # No need to pass/activate the parent Span, as
        # it stays in the context.
        with self.tracer.start_active_span('child'):
            raise gen.Return('%s::response' % message) 
开发者ID:opentracing,项目名称:opentracing-python,代码行数:7,代码来源:test_tornado.py

示例15: _request

# 需要导入模块: from tornado import gen [as 别名]
# 或者: from tornado.gen import Return [as 别名]
def _request(self, callback, request):
        try:
            response = yield self.client.fetch(request)
        except httpclient.HTTPError as e:
            if e.code == 599:
                raise base.Timeout
            response = e.response
        raise gen.Return(callback(self.response(response))) 
开发者ID:poppyred,项目名称:python-consul2,代码行数:10,代码来源:tornado.py


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