本文整理汇总了Python中tornado.concurrent.TracebackFuture.set_result方法的典型用法代码示例。如果您正苦于以下问题:Python TracebackFuture.set_result方法的具体用法?Python TracebackFuture.set_result怎么用?Python TracebackFuture.set_result使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类tornado.concurrent.TracebackFuture
的用法示例。
在下文中一共展示了TracebackFuture.set_result方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: wrapper
# 需要导入模块: from tornado.concurrent import TracebackFuture [as 别名]
# 或者: from tornado.concurrent.TracebackFuture import set_result [as 别名]
def wrapper(*args, **kwargs):
runner = None
future = TracebackFuture()
if 'callback' in kwargs:
callback = kwargs.pop('callback')
IOLoop.current().add_future(
future, lambda future: callback(future.result()))
def handle_exception(typ, value, tb):
try:
if runner is not None and runner.handle_exception(typ, value, tb):
return True
except Exception:
typ, value, tb = sys.exc_info()
future.set_exc_info((typ, value, tb))
return True
with ExceptionStackContext(handle_exception):
try:
result = func(*args, **kwargs)
except (Return, StopIteration) as e:
result = getattr(e, 'value', None)
except Exception:
future.set_exc_info(sys.exc_info())
return future
else:
if isinstance(result, types.GeneratorType):
def final_callback(value):
future.set_result(value)
runner = Runner(result, final_callback)
runner.run()
return future
future.set_result(result)
return future
示例2: StubWebSocketClientConnection
# 需要导入模块: from tornado.concurrent import TracebackFuture [as 别名]
# 或者: from tornado.concurrent.TracebackFuture import set_result [as 别名]
class StubWebSocketClientConnection(object):
"""A stub version of tornado.websocket.WebSocketClientConnection for use in
unit tests.
Attributes:
TIMEOUT: class constant, which should be overridden in subclasses, in
order to vary the time from when the class is instantiated until the
returned future is resolved.
connect_future: the Future resolved when the client "connection" is
made.
written_messages: a list of messages passed to the write_message
method to see they have been received.
"""
TIMEOUT = datetime.timedelta(milliseconds=0)
def __init__(self, io_loop, request, on_message_callback=None,
compression_options=None):
del request, on_message_callback, compression_options
self.connect_future = TracebackFuture()
self.written_messages = []
io_loop.add_timeout(self.TIMEOUT, self._complete)
def _complete(self):
self.connect_future.set_result(self)
def write_message(self, message):
"""Adds writen message to the written_messages buffer."""
self.written_messages.append(message)
示例3: wrapper
# 需要导入模块: from tornado.concurrent import TracebackFuture [as 别名]
# 或者: from tornado.concurrent.TracebackFuture import set_result [as 别名]
def wrapper(*args, **kwargs):
future = TracebackFuture()
try:
result = func(*args, **kwargs)
except (Return, StopIteration) as e:
result = getattr(e, 'value', None)
except Exception:
future.set_exc_info(sys.exc_info())
return future
else:
if isinstance(result, types.GeneratorType):
# Inline the first iteration of Runner.run. This lets us
# avoid the cost of creating a Runner when the coroutine
# never actually yields, which in turn allows us to
# use "optional" coroutines in critical path code without
# performance penalty for the synchronous case.
try:
orig_stack_contexts = stack_context._state.contexts
yielded = next(result)
if stack_context._state.contexts is not orig_stack_contexts:
yielded = TracebackFuture()
yielded.set_exception(
stack_context.StackContextInconsistentError(
'stack_context inconsistency (probably caused '
'by yield within a "with StackContext" block)'))
except (StopIteration, Return) as e:
future.set_result(getattr(e, 'value', None))
except Exception:
future.set_exc_info(sys.exc_info())
else:
# post runner into Cocaine ioloop
CocaineIO.instance().post(Runner, result, future, yielded)
return future
future.set_result(result)
return future
示例4: create
# 需要导入模块: from tornado.concurrent import TracebackFuture [as 别名]
# 或者: from tornado.concurrent.TracebackFuture import set_result [as 别名]
def create(self, param, req):
tableName = param['tableName'].name
initVal = param['initVal']
if tableName in self.tables:
raise KeyError, 'Table name already existed'
#print 'MASTER CREATE:', initVal
self.tables[tableName] = {'len': 0, }
#Data Partition
vals = {}
for worker_id in xrange(self.num_workers):
vals[worker_id] = {}
for key in initVal:
#Bookkeeping
self.tables[tableName]['len'] += 1
self.tables[tableName][key] = type(initVal[key])
worker = hash(key) % self.num_workers
vals[worker][key] = initVal[key]
futures = []
for worker_id in xrange(self.num_workers):
args = {'tableName': tableName, 'initVal': vals[worker_id]}
futures.append(self.client.fetch(formatQuery(self.workers[worker_id], 'create', args)))
fu = Future()
req.url = self.host
fu.set_result(HTTPResponse(req, 200, buffer=cStringIO.StringIO('OK')))
return fu
示例5: len
# 需要导入模块: from tornado.concurrent import TracebackFuture [as 别名]
# 或者: from tornado.concurrent.TracebackFuture import set_result [as 别名]
def len(self, param, req):
tableName = param['tableName'].name
if not tableName in self.tables:
raise KeyError, 'Table Not Found'
fu = Future()
req.url = self.host
fu.set_result(HTTPResponse(req, 200, buffer=cStringIO.StringIO(str(self.tables[tableName]['len']))))
return fu
示例6: fetch_all
# 需要导入模块: from tornado.concurrent import TracebackFuture [as 别名]
# 或者: from tornado.concurrent.TracebackFuture import set_result [as 别名]
def fetch_all(self, param, req):
tableName = param['tableName']
if not tableName in self.tables:
raise KeyError, 'Table Not Found!'
fu = Future()
req.url = self.host
fu.set_result(HTTPResponse(req, 200, buffer=cStringIO.StringIO(pickle.dumps(self.tables[tableName]))))
return fu
示例7: get
# 需要导入模块: from tornado.concurrent import TracebackFuture [as 别名]
# 或者: from tornado.concurrent.TracebackFuture import set_result [as 别名]
def get(self, param, req):
tableName = param['tableName']
key = param['key']
fu = Future()
req.url = self.host
fu.set_result(HTTPResponse(req, 200,
buffer=cStringIO.StringIO(pickle.dumps(self.tables[tableName][key]))))
return fu
示例8: close
# 需要导入模块: from tornado.concurrent import TracebackFuture [as 别名]
# 或者: from tornado.concurrent.TracebackFuture import set_result [as 别名]
def close(self):
if self._cursor is None:
future = TracebackFuture()
future.set_result(None)
return future
future = async_call_method(self._cursor.close)
def do_close(future):
self._cursor = None
future.add_done_callback(do_close)
return future
示例9: wrapper
# 需要导入模块: from tornado.concurrent import TracebackFuture [as 别名]
# 或者: from tornado.concurrent.TracebackFuture import set_result [as 别名]
def wrapper(*args, **kwargs):
future = TracebackFuture()
if replace_callback and "callback" in kwargs:
callback = kwargs.pop("callback")
IOLoop.current().add_future(future, lambda future: callback(future.result()))
try:
result = func(*args, **kwargs)
except (Return, StopIteration) as e:
result = _value_from_stopiteration(e)
except Exception:
future.set_exc_info(sys.exc_info())
return future
else:
if isinstance(result, GeneratorType):
# Inline the first iteration of Runner.run. This lets us
# avoid the cost of creating a Runner when the coroutine
# never actually yields, which in turn allows us to
# use "optional" coroutines in critical path code without
# performance penalty for the synchronous case.
try:
orig_stack_contexts = stack_context._state.contexts
yielded = next(result)
if stack_context._state.contexts is not orig_stack_contexts:
yielded = TracebackFuture()
yielded.set_exception(
stack_context.StackContextInconsistentError(
"stack_context inconsistency (probably caused "
'by yield within a "with StackContext" block)'
)
)
except (StopIteration, Return) as e:
future.set_result(_value_from_stopiteration(e))
except Exception:
future.set_exc_info(sys.exc_info())
else:
_futures_to_runners[future] = Runner(result, future, yielded)
try:
return future
finally:
# Subtle memory optimization: if next() raised an exception,
# the future's exc_info contains a traceback which
# includes this stack frame. This creates a cycle,
# which will be collected at the next full GC but has
# been shown to greatly increase memory usage of
# benchmarks (relative to the refcount-based scheme
# used in the absence of cycles). We can avoid the
# cycle by clearing the local variable after we return it.
future = None
future.set_result(result)
return future
示例10: set
# 需要导入模块: from tornado.concurrent import TracebackFuture [as 别名]
# 或者: from tornado.concurrent.TracebackFuture import set_result [as 别名]
def set(self, param, req):
tableName = param['tableName']
key = param['key']
val = param['val']
print 'Worker {0} SET: {1} with key {2} to {3}'.format(self.host, tableName, key, val)
self.tables[tableName][key] = val
fu = Future()
req.url = self.host
fu.set_result(HTTPResponse(req, 200, buffer=cStringIO.StringIO('OK')))
return fu
示例11: create
# 需要导入模块: from tornado.concurrent import TracebackFuture [as 别名]
# 或者: from tornado.concurrent.TracebackFuture import set_result [as 别名]
def create(self, param, req):
tableName = param['tableName']
initVal = param['initVal']
self.tables[tableName] = {}
for key in initVal:
self.tables[tableName][key] = initVal[key]
print 'Worker {0} CREATE:'.format(self.host), self.tables[tableName]
#Release
fu = Future()
req.url = self.host
fu.set_result(HTTPResponse(req, 200, buffer=cStringIO.StringIO('OK')))
return fu
示例12: fetch
# 需要导入模块: from tornado.concurrent import TracebackFuture [as 别名]
# 或者: from tornado.concurrent.TracebackFuture import set_result [as 别名]
def fetch(self, request, callback=None, **kwargs):
if not isinstance(request, HTTPRequest):
request = HTTPRequest(url=request, **kwargs)
response_partial = RequestCollection.find(request)
if response_partial:
resp = response_partial(request)
else:
resp = HTTPResponse(request, 404)
if callback is not None:
callback(resp)
else:
future = TracebackFuture()
future.set_result(resp)
return future
示例13: mock_fetch
# 需要导入模块: from tornado.concurrent import TracebackFuture [as 别名]
# 或者: from tornado.concurrent.TracebackFuture import set_result [as 别名]
def mock_fetch(self, request, callback=None, **kwargs):
future = TracebackFuture()
if callback is not None:
callback = stack_context.wrap(callback)
def handle_future(future):
response = future.result()
self.io_loop.add_callback(callback, response)
future.add_done_callback(handle_future)
res = MagicMock()
future.set_result(res)
return future
示例14: remove
# 需要导入模块: from tornado.concurrent import TracebackFuture [as 别名]
# 或者: from tornado.concurrent.TracebackFuture import set_result [as 别名]
def remove(self, param, req):
tableName = param['tableName']
key = param['key']
if not key in self.tables[tableName]:
return Future().set_result('Key Error')
if key is None:
del self.tables[tableName]
else:
del self.tables[tableName][key]
fu = Future()
req.url = self.host
fu.set_result(HTTPResponse(req, 200, buffer=cStringIO.StringIO('OK')))
return Future().set_result('OK')
示例15: wrapper
# 需要导入模块: from tornado.concurrent import TracebackFuture [as 别名]
# 或者: from tornado.concurrent.TracebackFuture import set_result [as 别名]
def wrapper(*args, **kwargs):
future = TracebackFuture()
# ipdb.set_trace()
print args, kwargs
# callback, args, kwargs = replacer.replace(lambda value=_NO_RESULT: future.set_result(value),args, kwargs)
callback = None
kwargs['callback'] = lambda value = _NO_RESULT: future.set_result(value)
print callback, args, kwargs
def handle_error(typ, value, tb):
future.set_exc_info((typ, value, tb))
return True
exc_info = None
with ExceptionStackContext(handle_error):
try:
result = f(*args, **kwargs)
if result is not None:
raise ReturnValueIgnoredError('@return_future should not be used with function that return values')
except:
exc_info = sys.exc_info()
raise
if exc_info is not None:
future.result()
if callback is not None:
def run_callback(future):
result = future.result()
if result is _NO_RESULT:
callback('result is NULL')
else:
callback(future.result())
future.add_done_callback(wrap(run_callback))
# print future.result(), 'in the wapper'
return future