本文整理汇总了Python中tornado.gen方法的典型用法代码示例。如果您正苦于以下问题:Python tornado.gen方法的具体用法?Python tornado.gen怎么用?Python tornado.gen使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类tornado
的用法示例。
在下文中一共展示了tornado.gen方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: retry
# 需要导入模块: import tornado [as 别名]
# 或者: from tornado import gen [as 别名]
def retry(*dargs, **dkw): # noqa
"""Wrap a function with a new `Retrying` object.
:param dargs: positional arguments passed to Retrying object
:param dkw: keyword arguments passed to the Retrying object
"""
# support both @retry and @retry() as valid syntax
if len(dargs) == 1 and callable(dargs[0]):
return retry()(dargs[0])
else:
def wrap(f):
if iscoroutinefunction is not None and iscoroutinefunction(f):
r = AsyncRetrying(*dargs, **dkw)
elif tornado and hasattr(tornado.gen, 'is_coroutine_function') \
and tornado.gen.is_coroutine_function(f):
r = TornadoRetrying(*dargs, **dkw)
else:
r = Retrying(*dargs, **dkw)
return r.wraps(f)
return wrap
示例2: read_full
# 需要导入模块: import tornado [as 别名]
# 或者: from tornado import gen [as 别名]
def read_full(stream):
"""Read the full contents of the given stream into memory.
:return:
A future containing the complete stream contents.
"""
assert stream, "stream is required"
chunks = []
chunk = yield stream.read()
while chunk:
if six.PY3 and isinstance(chunk, str):
chunk = chunk.encode('utf8')
chunks.append(chunk)
chunk = yield stream.read()
raise tornado.gen.Return(b''.join(chunks))
示例3: read
# 需要导入模块: import tornado [as 别名]
# 或者: from tornado import gen [as 别名]
def read(self):
if self.exception:
raise self.exception
if self.state == StreamState.completed or self._rpipe is None:
raise tornado.gen.Return(b"")
elif self.state == StreamState.init:
self.state = StreamState.streaming
chunk = b""
try:
chunk = yield self._rs.read_bytes(
common.MAX_PAYLOAD_SIZE, partial=True)
except StreamClosedError:
# reach the end of the pipe stream
self.state = StreamState.completed
finally:
if self.exception:
raise self.exception
raise tornado.gen.Return(chunk)
示例4: pytest_pyfunc_call
# 需要导入模块: import tornado [as 别名]
# 或者: from tornado import gen [as 别名]
def pytest_pyfunc_call(pyfuncitem):
gen_test_mark = pyfuncitem.get_closest_marker('gen_test')
if gen_test_mark:
io_loop = pyfuncitem.funcargs.get('io_loop')
run_sync = gen_test_mark.kwargs.get('run_sync', True)
funcargs = dict((arg, pyfuncitem.funcargs[arg])
for arg in _argnames(pyfuncitem.obj))
if iscoroutinefunction(pyfuncitem.obj):
coroutine = pyfuncitem.obj
future = tornado.gen.convert_yielded(coroutine(**funcargs))
else:
coroutine = tornado.gen.coroutine(pyfuncitem.obj)
future = coroutine(**funcargs)
if run_sync:
io_loop.run_sync(lambda: future, timeout=_timeout(pyfuncitem))
else:
# Run this test function as a coroutine, until the timeout. When completed, stop the IOLoop
# and reraise any exceptions
future_with_timeout = tornado.gen.with_timeout(
datetime.timedelta(seconds=_timeout(pyfuncitem)),
future)
io_loop.add_future(future_with_timeout, lambda f: io_loop.stop())
io_loop.start()
# This will reraise any exceptions that occurred.
future_with_timeout.result()
# prevent other pyfunc calls from executing
return True
示例5: get_arg
# 需要导入模块: import tornado [as 别名]
# 或者: from tornado import gen [as 别名]
def get_arg(context, index):
"""get value from arg stream in async way"""
if index < len(context.argstreams):
arg = b""
chunk = yield context.argstreams[index].read()
while chunk:
arg += chunk
chunk = yield context.argstreams[index].read()
raise tornado.gen.Return(arg)
else:
raise TChannelError()
示例6: get_header
# 需要导入模块: import tornado [as 别名]
# 或者: from tornado import gen [as 别名]
def get_header(self):
"""Get the application header value from the request.
:return: a future contains the deserialized value of header
"""
raw_header = yield get_arg(self, 1) # from arg2
if not self.serializer:
raise tornado.gen.Return(raw_header)
else:
header = self.serializer.deserialize_header(raw_header)
raise tornado.gen.Return(header)
示例7: get_body
# 需要导入模块: import tornado [as 别名]
# 或者: from tornado import gen [as 别名]
def get_body(self):
"""Get the body value from the request.
:return: a future contains the deserialized value of body
"""
raw_body = yield get_arg(self, 2) # from arg3
if not self.serializer:
raise tornado.gen.Return(raw_body)
else:
body = self.serializer.deserialize_body(raw_body)
raise tornado.gen.Return(body)
示例8: get_body
# 需要导入模块: import tornado [as 别名]
# 或者: from tornado import gen [as 别名]
def get_body(self):
"""Get the body value from the response.
:return: a future contains the deserialized value of body
"""
raw_body = yield get_arg(self, 2)
if not self.serializer:
raise tornado.gen.Return(raw_body)
else:
body = self.serializer.deserialize_body(raw_body)
raise tornado.gen.Return(body)
示例9: handler_error
# 需要导入模块: import tornado [as 别名]
# 或者: from tornado import gen [as 别名]
def handler_error(request, response):
yield tornado.gen.sleep(0.01)
yield response.connection.send_error(BusyError("retry", request.id))
# stop normal response streams
response.set_exception(TChannelError("stop stream"))
示例10: chain
# 需要导入模块: import tornado [as 别名]
# 或者: from tornado import gen [as 别名]
def chain(number_of_peers, endpoint):
tchannel = TChannel(name='test')
for i in range(number_of_peers):
# Connect to these peers so that they are treated as higher priority
# than other peers.
yield tchannel.peers.get(server(endpoint).hostport).connect()
raise tornado.gen.Return(tchannel)
示例11: register
# 需要导入模块: import tornado [as 别名]
# 或者: from tornado import gen [as 别名]
def register(tchannel):
@tchannel.json.register("json_echo")
@tornado.gen.coroutine
def json_echo(request):
headers = request.headers
body = request.body
return Response(body, headers)
示例12: test_span_tags
# 需要导入模块: import tornado [as 别名]
# 或者: from tornado import gen [as 别名]
def test_span_tags(encoding, operation, tracer, thrift_service):
server = TChannel('server', tracer=tracer)
server.listen()
def get_span_baggage():
sp = server.context_provider.get_current_span()
baggage = sp.get_baggage_item('bender') if sp else None
return {'bender': baggage}
@server.json.register('foo')
def handler(_):
return get_span_baggage()
@server.thrift.register(thrift_service.X, method='thrift2')
def thrift2(_):
return json.dumps(get_span_baggage())
client = TChannel('client', tracer=tracer, trace=True)
opentracing.set_global_tracer(tracer)
span = tracer.start_span('root')
span.set_baggage_item('bender', 'is great')
with span:
res = None
with client.context_provider.span_in_context(span):
if encoding == 'json':
res = client.json(
service='test-service', # match thrift_service name
endpoint='foo',
body={},
hostport=server.hostport,
)
elif encoding == 'thrift':
res = client.thrift(
thrift_service.X.thrift2(),
hostport=server.hostport,
)
else:
raise ValueError('Unknown encoding %s' % encoding)
res = yield res # cannot yield in StackContext
res = res.body
if isinstance(res, six.string_types):
res = json.loads(res)
assert res == {'bender': 'is great'}
for i in range(1000):
spans = tracer.reporter.get_spans()
if len(spans) == 3:
break
yield tornado.gen.sleep(0.001) # yield execution and sleep for 1ms
spans = tracer.reporter.get_spans()
assert len(spans) == 3
trace_ids = set([s.trace_id for s in spans])
assert 1 == len(trace_ids), \
'all spans must have the same trace_id: %s' % trace_ids