def create_attribute(self, cls, attr_name):
async_method = self.property.create_attribute(cls, attr_name)
original_class = self.original_class
@functools.wraps(async_method)
@motor_coroutine
def wrapper(self, *args, **kwargs):
result = yield async_method(self, *args, **kwargs)
# Don't call isinstance(), not checking subclasses.
if result.__class__ == original_class:
# Delegate to the current object to wrap the result.
raise gen.Return(self.wrap(result))
else:
raise gen.Return(result)
if self.doc:
wrapper.__doc__ = self.doc
return wrapper
python类Return()的实例源码
def resolve(self, host, port, family):
"""Return list of (family, address) pairs."""
child_gr = greenlet.getcurrent()
main = child_gr.parent
assert main is not None, "Should be on child greenlet"
def handler(exc_typ, exc_val, exc_tb):
# If netutil.Resolver is configured to use TwistedResolver.
if DomainError and issubclass(exc_typ, DomainError):
exc_typ = socket.gaierror
exc_val = socket.gaierror(str(exc_val))
# Depending on the resolver implementation, we could be on any
# thread or greenlet. Return to the loop's thread and raise the
# exception on the calling greenlet from there.
self.io_loop.add_callback(functools.partial(
child_gr.throw, exc_typ, exc_val, exc_tb))
return True # Don't propagate the exception.
with stack_context.ExceptionStackContext(handler):
self.resolver.resolve(host, port, family, callback=child_gr.switch)
return main.switch()
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)
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
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
def resolve(self, host, port, family):
"""Return list of (family, address) pairs."""
child_gr = greenlet.getcurrent()
main = child_gr.parent
assert main is not None, "Should be on child greenlet"
def handler(exc_typ, exc_val, exc_tb):
# If netutil.Resolver is configured to use TwistedResolver.
if DomainError and issubclass(exc_typ, DomainError):
exc_typ = socket.gaierror
exc_val = socket.gaierror(str(exc_val))
# Depending on the resolver implementation, we could be on any
# thread or greenlet. Return to the loop's thread and raise the
# exception on the calling greenlet from there.
self.io_loop.add_callback(functools.partial(
child_gr.throw, exc_typ, exc_val, exc_tb))
return True # Don't propagate the exception.
with stack_context.ExceptionStackContext(handler):
self.resolver.resolve(host, port, family, callback=child_gr.switch)
return main.switch()
def create_attribute(self, cls, attr_name):
async_method = self.property.create_attribute(cls, attr_name)
original_class = self.original_class
@functools.wraps(async_method)
@motor_coroutine
def wrapper(self, *args, **kwargs):
result = yield async_method(self, *args, **kwargs)
# Don't call isinstance(), not checking subclasses.
if result.__class__ == original_class:
# Delegate to the current object to wrap the result.
raise gen.Return(self.wrap(result))
else:
raise gen.Return(result)
if self.doc:
wrapper.__doc__ = self.doc
return wrapper
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)
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
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
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
def resolve(self, host, port, family):
"""Return list of (family, address) pairs."""
child_gr = greenlet.getcurrent()
main = child_gr.parent
assert main is not None, "Should be on child greenlet"
def handler(exc_typ, exc_val, exc_tb):
# If netutil.Resolver is configured to use TwistedResolver.
if DomainError and issubclass(exc_typ, DomainError):
exc_typ = socket.gaierror
exc_val = socket.gaierror(str(exc_val))
# Depending on the resolver implementation, we could be on any
# thread or greenlet. Return to the loop's thread and raise the
# exception on the calling greenlet from there.
self.io_loop.add_callback(functools.partial(
child_gr.throw, exc_typ, exc_val, exc_tb))
return True # Don't propagate the exception.
with stack_context.ExceptionStackContext(handler):
self.resolver.resolve(host, port, family, callback=child_gr.switch)
return main.switch()
def create_attribute(self, cls, attr_name):
async_method = self.property.create_attribute(cls, attr_name)
original_class = self.original_class
@functools.wraps(async_method)
@motor_coroutine
def wrapper(self, *args, **kwargs):
result = yield async_method(self, *args, **kwargs)
# Don't call isinstance(), not checking subclasses.
if result.__class__ == original_class:
# Delegate to the current object to wrap the result.
raise gen.Return(self.wrap(result))
else:
raise gen.Return(result)
if self.doc:
wrapper.__doc__ = self.doc
return wrapper
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)
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
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
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
def resolve(self, host, port, family):
"""Return list of (family, address) pairs."""
child_gr = greenlet.getcurrent()
main = child_gr.parent
assert main is not None, "Should be on child greenlet"
def handler(exc_typ, exc_val, exc_tb):
# If netutil.Resolver is configured to use TwistedResolver.
if DomainError and issubclass(exc_typ, DomainError):
exc_typ = socket.gaierror
exc_val = socket.gaierror(str(exc_val))
# Depending on the resolver implementation, we could be on any
# thread or greenlet. Return to the loop's thread and raise the
# exception on the calling greenlet from there.
self.io_loop.add_callback(functools.partial(
child_gr.throw, exc_typ, exc_val, exc_tb))
return True # Don't propagate the exception.
with stack_context.ExceptionStackContext(handler):
self.resolver.resolve(host, port, family, callback=child_gr.switch)
return main.switch()
def create_attribute(self, cls, attr_name):
async_method = self.property.create_attribute(cls, attr_name)
original_class = self.original_class
@functools.wraps(async_method)
@motor_coroutine
def wrapper(self, *args, **kwargs):
result = yield async_method(self, *args, **kwargs)
# Don't call isinstance(), not checking subclasses.
if result.__class__ == original_class:
# Delegate to the current object to wrap the result.
raise gen.Return(self.wrap(result))
else:
raise gen.Return(result)
if self.doc:
wrapper.__doc__ = self.doc
return wrapper
def _read_socks5_address(self):
atyp = yield self.read_bytes(1)
if atyp == b"\x01":
data = yield self.read_bytes(4)
addr = socket.inet_ntoa(data)
elif atyp == b"\x03":
length = yield self.read_bytes(1)
addr = yield self.read_bytes(length)
elif atyp == b"\x04":
data = yield self.read_bytes(16)
addr = socket.inet_ntop(socket.AF_INET6, data)
else:
raise GeneralProxyError("SOCKS5 proxy server sent invalid data")
data = yield self.read_bytes(2)
port = struct.unpack(">H", data)[0]
raise gen.Return((addr, port))
def _read_stream_body(self, content_length, delegate):
while 0 < content_length:
try:
body = yield self.stream.read_bytes(
min(self.params.chunk_size, content_length), partial=True)
except StreamClosedError:
# with partial stream will update close status after receiving
# the last chunk, so we catch StreamClosedError instead
raise gen.Return(False)
content_length -= len(body)
if not self._write_finished or self.is_client:
with _ExceptionLoggingContext(app_log):
ret = delegate.data_received(body)
if ret is not None:
yield ret
raise gen.Return(True)
def apparent_encoding(self):
"""The apparent encoding, provided by the chardet library."""
def _encoding(content):
return chardet.detect(content)['encoding']
@gen.coroutine
def _stream_apparent_encoding():
content = yield self.content
raise Return(_encoding(content))
if not isinstance(self.raw, HTTPMessageDelegate):
raise TypeError('self.raw must be a trip.adapters.MessageDelegate')
if self.raw.stream:
return _stream_apparent_encoding()
else:
return _encoding(self.content)
def make_connection(self, endpoint, api_path):
conn = Connection(endpoint, api_path, self._event_handler,
principal=self.principal, secret=self.secret)
try:
yield conn.ping()
except MasterRedirect as ex: # pragma: no cover
if ex.location == self.master_info.current_location:
log.warn('Leading Master not elected yet')
else: # pragma: no cover
log.warn('Master not leading')
self.master_info.redirected_uri(ex.location)
conn = None
except ConnectionRefusedError as ex: # pragma: no cover
conn = None
except Exception: # pragma: no cover
conn = None
raise gen.Return(conn)
def get_instances(matrix_api=None):
"""
?????matrix?API?????????
??????????????????
"""
try:
# response = yield httpclient.AsyncHTTPClient().fetch(MATRIX_API_GET_INSTANCES)
# handle matrix response here
# ????????????matrix??
conf = yaml.safe_load(open(ALERT_CONF))
instances = conf['instances']
except Exception as e:
logging.error(e, exc_info=True)
raise gen.Return([])
raise gen.Return(instances)
def benchmark():
client = SimpleAsyncHTTP2Client(
host=options.h, port=options.p,
secure=options.s, max_streams=options.c,
connect_timeout=5, enable_push=False,
initial_window_size=2**24-1,
)
start = time.time()
futures = []
for i in range(options.n):
futures.append(client.fetch('/'))
yield futures
end = time.time()
raise gen.Return(end - start)
def get_db_object_by_attr(object_, **kwargs):
assert len(kwargs) >= 1, 'function get_db_object_by_attr need argument'
if 'ignore' in kwargs:
ignore = kwargs.pop('ignore')
else:
ignore = False
if len(kwargs) > 1:
filter_ = and_(*[getattr(object_, key) == value
for key, value in kwargs.iteritems()])
else:
key, value = kwargs.popitem()
filter_ = getattr(object_, key) == value
user = yield execute(sqls=[('query', object_),
('filter', filter_),
('first', None)])
if not user and not ignore:
raise gen.Return(invalid_argument_error('wrong %s' % key))
raise gen.Return(user)
def generate_invitingcode():
random_seed = [
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p',
'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'z',
'x', 'c', 'v', 'b', 'n', 'm', 'P', 'O', 'I', 'U',
'Y', 'T', 'R', 'E', 'W', 'Q', 'A', 'S', 'D', 'F',
'G', 'H', 'J', 'K', 'L', 'M', 'N', 'B', 'V', 'C',
'X', 'Z'
]
code = "".join(random.sample(random_seed, random.randint(4, 8)))
while True:
temp = yield get_db_object_by_attr(User, code=code, ignore=True)
if not temp:
break
code = "".join(random.sample(random_seed, random.randint(4, 8)))
raise gen.Return(code)
def put_user(user, nickname=None, headimg=None, sex=None, introduction=None):
if nickname and nickname != user.nickname:
if (yield get_db_object_by_attr(User, nickname=nickname, ignore=True)) is not None:
raise gen.Return(already_exist_error('nickname %s is already existed' % nickname))
user.nickname = nickname
if headimg:
if user.headimg != DEFAULT_HEADIMG:
result = yield remove_image_from_oss(user.headimg)
if not result:
LOG.error('failed to remove image: %s' % user.headimg)
headimg_path = yield save_image_to_oss(headimg, OSS_HEADIMG_PATH,
str(datetime.now()), when_fail=DEFAULT_HEADIMG)
user.headimg = headimg_path
if sex:
user.sex = sex
if introduction:
user.introduction = introduction
yield execute(('add', user))
yield execute(('commit', None))
def save_image_to_oss(file_, storge_path, key, when_fail=None):
if not allow_image_format(file_["filename"]):
raise gen.Return(invalid_argument_error('invalid image format: only jpg, ipeg, png is supported'))
if not allow_image_size(file_):
raise gen.Return(invalid_argument_error('invalid image size: less than or equal 2M is required'))
seed = hash_str(os.path.splitext(file_["filename"])[0] + key)
image_filename = storge_path + seed + os.path.splitext(file_["filename"])[1]
result = yield CommonTaskFactory. \
get_task(TaskNames.PROCESS_IMAGE.value). \
run('upload_file', file_["body"], image_filename)
if result:
raise gen.Return(image_filename)
else:
raise gen.Return(when_fail)
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)