Python tornado.gen.Return() Examples
The following are 30
code examples of tornado.gen.Return().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
tornado.gen
, or try the search function
.
Example #1
Source File: tcpclient.py From tornado-zh with MIT License | 7 votes |
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)
Example #2
Source File: gen_test.py From tornado-zh with MIT License | 6 votes |
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
Example #3
Source File: gen_test.py From tornado-zh with MIT License | 6 votes |
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
Example #4
Source File: httpmodule.py From torngas with BSD 3-Clause "New" or "Revised" License | 6 votes |
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)
Example #5
Source File: evaluation_plane_handler.py From TabPy with MIT License | 6 votes |
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)
Example #6
Source File: gen_test.py From opendevops with GNU General Public License v3.0 | 6 votes |
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
Example #7
Source File: gen_test.py From opendevops with GNU General Public License v3.0 | 6 votes |
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
Example #8
Source File: tcpclient.py From tornado-zh with MIT License | 6 votes |
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)
Example #9
Source File: gen_test.py From tornado-zh with MIT License | 6 votes |
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
Example #10
Source File: gen_test.py From tornado-zh with MIT License | 6 votes |
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
Example #11
Source File: twisted.py From viewfinder with Apache License 2.0 | 6 votes |
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)
Example #12
Source File: webspider.py From tornado-zh with MIT License | 6 votes |
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)
Example #13
Source File: TTornado.py From galaxy-sdk-python with Apache License 2.0 | 6 votes |
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)
Example #14
Source File: tornado-crawler-demo1.py From Python_Master_Courses with GNU General Public License v3.0 | 6 votes |
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)
Example #15
Source File: auth.py From viewfinder with Apache License 2.0 | 5 votes |
def _oauth_get_user_future(self, access_token): user = yield self.twitter_request( "/account/verify_credentials", access_token=access_token) if user: user["username"] = user["screen_name"] raise gen.Return(user)
Example #16
Source File: gen_test.py From viewfinder with Apache License 2.0 | 5 votes |
def test_sync_raise_return(self): # gen.Return is allowed in @gen.engine, but it may not be used # to return a value. @gen.engine def f(): self.stop(42) raise gen.Return() result = self.run_gen(f) self.assertEqual(result, 42)
Example #17
Source File: gen_test.py From opendevops with GNU General Public License v3.0 | 5 votes |
def test_sync_gen_return(self): @gen.coroutine def f(): raise gen.Return(42) result = yield f() self.assertEqual(result, 42) self.finished = True
Example #18
Source File: gen_test.py From opendevops with GNU General Public License v3.0 | 5 votes |
def test_async_raise_return_value_tuple(self): @gen.coroutine def f(): yield gen.moment raise gen.Return((1, 2)) self.assertEqual((1, 2), self.io_loop.run_sync(f))
Example #19
Source File: gen_test.py From opendevops with GNU General Public License v3.0 | 5 votes |
def test_multi_moment(self): # Test gen.multi with moment # now that it's not a real Future @gen.coroutine def wait_a_moment(): result = yield gen.multi([gen.moment, gen.moment]) raise gen.Return(result) loop = self.get_new_ioloop() result = loop.run_sync(wait_a_moment) self.assertEqual(result, [None, None])
Example #20
Source File: caresresolver.py From viewfinder with Apache License 2.0 | 5 votes |
def resolve(self, host, port, family=0): if is_valid_ip(host): addresses = [host] else: # gethostbyname doesn't take callback as a kwarg self.channel.gethostbyname(host, family, (yield gen.Callback(1))) callback_args = yield gen.Wait(1) assert isinstance(callback_args, gen.Arguments) assert not callback_args.kwargs result, error = callback_args.args if error: raise Exception('C-Ares returned error %s: %s while resolving %s' % (error, pycares.errno.strerror(error), host)) addresses = result.addresses addrinfo = [] for address in addresses: if '.' in address: address_family = socket.AF_INET elif ':' in address: address_family = socket.AF_INET6 else: address_family = socket.AF_UNSPEC if family != socket.AF_UNSPEC and family != address_family: raise Exception('Requested socket family %d but got %d' % (family, address_family)) addrinfo.append((address_family, (address, port))) raise gen.Return(addrinfo)
Example #21
Source File: gen_test.py From opendevops with GNU General Public License v3.0 | 5 votes |
def test_async_raise_return_value(self): @gen.coroutine def f(): yield gen.moment raise gen.Return(42) self.assertEqual(42, self.io_loop.run_sync(f))
Example #22
Source File: gen_test.py From opendevops with GNU General Public License v3.0 | 5 votes |
def test_sync_raise_return_value_tuple(self): @gen.coroutine def f(): raise gen.Return((1, 2)) self.assertEqual((1, 2), self.io_loop.run_sync(f))
Example #23
Source File: gen_test.py From opendevops with GNU General Public License v3.0 | 5 votes |
def test_async_raise_return(self): @gen.coroutine def f(): yield gen.moment raise gen.Return() self.io_loop.run_sync(f)
Example #24
Source File: gen_test.py From opendevops with GNU General Public License v3.0 | 5 votes |
def test_sync_raise_return(self): @gen.coroutine def f(): raise gen.Return() self.io_loop.run_sync(f)
Example #25
Source File: gen_test.py From opendevops with GNU General Public License v3.0 | 5 votes |
def add_one_async(self, x): yield gen.moment raise gen.Return(x + 1)
Example #26
Source File: gen_test.py From opendevops with GNU General Public License v3.0 | 5 votes |
def delay(self, iterations, arg): """Returns arg after a number of IOLoop iterations.""" for i in range(iterations): yield gen.moment raise gen.Return(arg)
Example #27
Source File: websocket_test.py From opendevops with GNU General Public License v3.0 | 5 votes |
def ws_connect(self, path, **kwargs): ws = yield websocket_connect( "ws://127.0.0.1:%d%s" % (self.get_http_port(), path), **kwargs ) raise gen.Return(ws)
Example #28
Source File: TTornado.py From galaxy-sdk-python with Apache License 2.0 | 5 votes |
def readFrame(self): # IOStream processes reads one at a time with (yield self._read_lock.acquire()): with self.io_exception_context(): frame_header = yield self.stream.read_bytes(4) if len(frame_header) == 0: raise iostream.StreamClosedError('Read zero bytes from stream') frame_length, = struct.unpack('!i', frame_header) frame = yield self.stream.read_bytes(frame_length) raise gen.Return(frame)
Example #29
Source File: TTornado.py From galaxy-sdk-python with Apache License 2.0 | 5 votes |
def acquire(self): blocker = self._waiters[-1] if self.acquired() else None future = concurrent.Future() self._waiters.append(future) if blocker: yield blocker raise gen.Return(self._lock_context())
Example #30
Source File: base.py From python-xbee with MIT License | 5 votes |
def wait_read_frame(self, timeout=None): frame = yield self._get_frame(timeout=timeout) raise gen.Return(self._split_response(frame.data))