本文整理汇总了Python中tornado.concurrent.Future.result方法的典型用法代码示例。如果您正苦于以下问题:Python Future.result方法的具体用法?Python Future.result怎么用?Python Future.result使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类tornado.concurrent.Future
的用法示例。
在下文中一共展示了Future.result方法的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: error_callback
# 需要导入模块: from tornado.concurrent import Future [as 别名]
# 或者: from tornado.concurrent.Future import result [as 别名]
def error_callback(future: Future) -> None:
try:
future.result()
except Exception as e:
if not isinstance(e, quiet_exceptions):
app_log.error(
"Exception in Future %r after timeout", future, exc_info=True
)
示例2: UploadToProvider
# 需要导入模块: from tornado.concurrent import Future [as 别名]
# 或者: from tornado.concurrent.Future import result [as 别名]
class UploadToProvider(BaseHandler):
'''
Upload the file associated with the given mobius id to all providers.
'''
def initialize(self, loop):
self._loop = loop
self._request_dealer = SocketFactory.dealer_socket("/request/local",
on_recv=self._process_result,
transport=stream.INPROC,
bind=False,
loop=loop)
self._request_future = None
def _process_result(self, envelope, msgs):
response = msgs[-1]
self._request_future.set_result(response)
@gen.coroutine
def get(self):
log.info("UploadToProvider handler get")
self._request_future = Future()
mobius_id = int(self.get_argument("mobius_id", default=0))
user_id = self.current_user.id
params = JSONObject()
params.mobius_id = mobius_id
params.user_id = user_id
request = Request(command=Command.UPLOAD.value,
params=params.json_string)
self._request_dealer.send(request)
log.info("Lets wait here one second.")
yield self._request_future
log.info("One second should have passed.")
response = self._request_future.result()
state = response.state
while state.state_id == UPLOADING:
result = JSONObject(state.response)
log.info("Progress: {}".format(result.progress))
self._request_future = Future()
yield self._request_future
response = self._request_future.result()
state = response.state
if state.state_id == ERROR:
self.set_status(500)
self.write(state.error)
elif state.state_id == RESULT:
self.set_status(200)
self.write(state.response)
else:
self.set_status(500)
self.write("Unexpected error occurred.")
self._request_future = None
示例3: test_future_set_result_unless_cancelled
# 需要导入模块: from tornado.concurrent import Future [as 别名]
# 或者: from tornado.concurrent.Future import result [as 别名]
def test_future_set_result_unless_cancelled(self):
fut = Future()
future_set_result_unless_cancelled(fut, 42)
self.assertEqual(fut.result(), 42)
self.assertFalse(fut.cancelled())
fut = Future()
fut.cancel()
is_cancelled = fut.cancelled()
future_set_result_unless_cancelled(fut, 42)
self.assertEqual(fut.cancelled(), is_cancelled)
if not is_cancelled:
self.assertEqual(fut.result(), 42)
示例4: test_notify_waiter
# 需要导入模块: from tornado.concurrent import Future [as 别名]
# 或者: from tornado.concurrent.Future import result [as 别名]
def test_notify_waiter(self):
brewhouse = Brewhouse.objects.create(name="Foo")
recipe = Recipe.objects.create(name="Bar")
instance = RecipeInstance.objects.create(recipe=recipe)
future = Future()
recipe_instance.RecipeInstanceHandler.waiters[brewhouse.pk] = [future]
recipe_instance.RecipeInstanceHandler.notify(brewhouse.pk, instance.pk)
self.assertEquals(future.result(), {'recipe_instance': instance.pk})
示例5: test_finish
# 需要导入模块: from tornado.concurrent import Future [as 别名]
# 或者: from tornado.concurrent.Future import result [as 别名]
def test_finish(self):
f = Future()
ag = AsyncGroup(partial(f.set_result, True))
self.assertEqual(ag._finished, False)
ag.add_notification()
ag.finish()
self.assertEqual(ag._finished, True)
self.assertEqual(f.result(), True)
示例6: generate_challenge
# 需要导入模块: from tornado.concurrent import Future [as 别名]
# 或者: from tornado.concurrent.Future import result [as 别名]
def generate_challenge(self):
"""Returns authentication challenge message, with OAuth URL."""
nonce = uuid.uuid4().hex
future = Future()
future.add_done_callback(lambda future: self.session.transport.write_json(
self.authenticate(future.result())))
self.session.properties.futures[nonce] = future
return {
"url": "http://robust.brendan.so/auth/twitter?robust_token=%s" % nonce
}
示例7: QuoteHandler
# 需要导入模块: from tornado.concurrent import Future [as 别名]
# 或者: from tornado.concurrent.Future import result [as 别名]
class QuoteHandler(BaseHandler):
'''
Remove me.
'''
def initialize(self, loop):
self._loop = loop
self._request_dealer = SocketFactory.dealer_socket("/request/local",
on_recv=self._process_result,
transport=stream.INPROC,
bind=False,
loop=loop)
self._request_future = None
def _process_result(self, envelope, msgs):
response = msgs[-1]
self._request_future.set_result(response)
@authenticated
@gen.coroutine
def get(self):
log.info("Test handler get")
self._request_future = Future()
mobius_id = int(self.get_argument("mobius_id", default=1))
user_id = self.current_user.id
params = JSONObject()
params.mobius_id = mobius_id
params.user_id = user_id
params.http_params = {Parameter.QUANTITY.name: 1,
Parameter.SCALE.name: 0.1,
Parameter.UNIT.name: "cm"}
# Parameter.MATERIAL.name: "metal_cast_silver_sanded"}
request = Request(command=Command.QUOTE.value,
params=params.json_string)
self._request_dealer.send(request)
log.info("Lets wait here one second.")
yield self._request_future
log.info("One second should have passed.")
response = self._request_future.result()
state = response.state
if state.state_id == ERROR:
self.set_status(500)
self.write(state.error)
elif state.state_id == RESULT:
self.set_status(200)
self.write(state.response)
else:
self.set_status(500)
self.write("Unexpected error occurred.")
self._request_future = None
示例8: test_notify_waiter_only_correct_brewhouse
# 需要导入模块: from tornado.concurrent import Future [as 别名]
# 或者: from tornado.concurrent.Future import result [as 别名]
def test_notify_waiter_only_correct_brewhouse(self):
brewhouse1 = Brewhouse.objects.create(name="Foo")
brewhouse2 = Brewhouse.objects.create(name="Baz")
recipe = Recipe.objects.create(name="Bar")
instance = RecipeInstance.objects.create(recipe=recipe)
future1 = Future()
future2 = Future()
recipe_instance.RecipeInstanceHandler.waiters[brewhouse1.pk] = [future1]
recipe_instance.RecipeInstanceHandler.waiters[brewhouse2.pk] = [future2]
recipe_instance.RecipeInstanceHandler.notify(brewhouse1.pk, instance.pk)
self.assertEquals(future1.result(), {'recipe_instance': instance.pk})
self.assertFalse(future2.done())
示例9: test_await_future
# 需要导入模块: from tornado.concurrent import Future [as 别名]
# 或者: from tornado.concurrent.Future import result [as 别名]
def test_await_future(self):
f = Future()
tf = TornadoFuture()
def finish_later():
time.sleep(0.1)
f.set_result('future')
tf.set_result('tornado')
Thread(target=finish_later).start()
assert self.client.wait([f, tf])
assert f.done()
assert tf.done()
assert f.result() == 'future'
assert tf.result() == 'tornado'
示例10: test_notifications
# 需要导入模块: from tornado.concurrent import Future [as 别名]
# 或者: from tornado.concurrent.Future import result [as 别名]
def test_notifications(self):
f = Future()
ag = AsyncGroup(partial(f.set_result, True))
not1 = ag.add_notification()
not2 = ag.add_notification()
self.assertEqual(ag._finish_cb_called, False)
not1()
self.assertEqual(ag._finish_cb_called, False)
not2('params', are='ignored')
self.assertEqual(ag._finish_cb_called, True)
self.assertEqual(ag._aborted, False)
self.assertEqual(f.result(), True)
示例11: make_request
# 需要导入模块: from tornado.concurrent import Future [as 别名]
# 或者: from tornado.concurrent.Future import result [as 别名]
def make_request(message, socket):
'''
Helper method to make async service requests.
@param message - message to be sent to service
@param socket - socket to send it on
@returns service response message
'''
future = Future()
# set result callback
socket.on_recv(lambda _, msgs: future.set_result(msgs[-1]))
socket.send(message)
yield future
# clear result callback
socket.on_recv(None)
return future.result()
示例12: test_notifications
# 需要导入模块: from tornado.concurrent import Future [as 别名]
# 或者: from tornado.concurrent.Future import result [as 别名]
def test_notifications(self):
f = Future()
ag = AsyncGroup(partial(f.set_result, True))
not1 = ag.add_notification()
not2 = ag.add_notification()
self.assertEqual(ag._finished, False)
not1()
self.assertEqual(ag._finished, False)
not2('params', are='ignored')
self.assertEqual(ag._finished, True)
self.assertEqual(f.result(), True)
with ExpectLog(async_logger, r'.*trying to finish already finished AsyncGroup\(name=None, finished=True\)'):
ag.finish()
示例13: _discard_future_result
# 需要导入模块: from tornado.concurrent import Future [as 别名]
# 或者: from tornado.concurrent.Future import result [as 别名]
def _discard_future_result(self, future: Future) -> None:
"""Avoid unhandled-exception warnings from spawned coroutines."""
future.result()
示例14: StreamHandler
# 需要导入模块: from tornado.concurrent import Future [as 别名]
# 或者: from tornado.concurrent.Future import result [as 别名]
class StreamHandler(PostContentHandler):
'''
Upload file request handler. It downloads the file in streaming mode from
the client, and stores the downloaded files in the provided temporary
directory.
'''
DISPOSITION = "Content-Disposition"
FILE_FIELD = "fileID"
NAME_FIELD = "fileName"
POST_SUCCESS = 201
def initialize(self, loop):
'''
The initialization method for this request handler.
@param loop - zmq eventloop
'''
super(StreamHandler, self).initialize()
self._tmp_file = tempfile.NamedTemporaryFile(dir=get_tmp_dir(), delete=False)
self._upload_pub = SocketFactory.dealer_socket("/db/request",
bind=False,
on_recv=self._handle_response,
loop=loop)
self._cur_headers = None
self._file_started = False
self._user_file_name = None
self._upload_future = None
self._uploaded_model_id = None
def get_current_user(self):
'''
Fetch current users information.
'''
user = escape.to_basestring(self.get_secure_cookie("mobius_user"))
if user is None:
return None
return JSONObject(user)
def _handle_response(self, envelope, msgs):
'''
Handle the response from the database.
@param envelope - a list of router socket ids
@param msgs - list of messages
'''
self._upload_future.set_result(msgs[-1])
def on_finish(self):
'''
Do the clean ups here.
'''
self._tmp_file.close()
def post(self):
'''
Overriden post method to respond to the client.
'''
self.set_header("Content-Type", "text/plain")
self.set_status(self.POST_SUCCESS)
if self._uploaded_model_id is not None:
self.write(json.dumps({"success": True,
"model_id": self._uploaded_model_id}))
else:
self.write(json.dumps({"success": False,
"model_id": -1}))
self.finish()
def _write_file_data(self, headers, data):
'''
Writes the data to the temporary file.
@param headers - HTTP headers for this file
@param data - a chunk of file data
'''
self._tmp_file.write(data)
def _process_form_field(self, headers, data):
'''
Saves the data entered into the upload form.
@param headers - HTTP headers for this form field
@param data - data entered into the field.
'''
if self._get_field_name(headers) == self.NAME_FIELD:
self._user_file_name = data.decode(self.HEADER_ENCODING)
else:
log.info("Field name: {}".format(self._get_field_name(headers)))
def _get_field_name(self, headers):
'''
Retrieve the name of the field from the given headers.
@param headers - HTTP headers
@returns name of the field specified on the page
'''
try:
return headers[self.DISPOSITION]["params"]["name"]
except KeyError:
return None
#.........这里部分代码省略.........