本文整理汇总了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)
示例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)
示例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
示例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
示例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
示例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)
示例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)
示例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
示例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)
示例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)
示例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)
示例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)
示例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)
示例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)
示例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)))