如何从 tornado.ioloop.IOLoop 实例 add_future 方法中获取 return 值
how to return value from tornado.ioloop.IOLoop instance add_future method
我正在使用 RethinkDB
和 Tornado
以及异步方法。基于我在 RethinkDB
中的数据模型,在为我的 topic
table 插入一条记录后,我还必须 update/insert 新记录到 user_to_topic
table.这是我的 post 请求处理程序的基本设置。
class TopicHandler(tornado.web.RequestHandler):
def get(self):
pass
@gen.coroutine
def post(self, *args, **kwargs):
# to establish databse connection
connection = rth.connect(host='localhost', port=00000, db=DATABASE_NAME)
# Thread the connection
threaded_conn = yield connection
title = self.get_body_argument('topic_title')
# insert into table
new_topic_record = rth.table('Topic').insert({
'title': topic_title,
}, conflict="error",
durability="hard").run(threaded_conn)
# {TODO} for now assume the result is always written successfully
io_loop = ioloop.IOLoop.instance()
# I want to return my generated_keys here
io_loop.add_future(new_topic_record, self.return_written_record_id)
# do stuff with the generated_keys here, how do I get those keys
def return_written_record_id(self, f):
_result = f.result()
return _result['generated_keys']
插入操作完成后,具有 return 的 Future
对象来自 RethinkDB
通过 Future.result()
方法插入操作的结果,我可以使用 [=检索新记录的 ID =22=] 结果的属性。根据 Tornado
document,我可以在我的回调函数 return_written_record_id
中处理结果。当然,我可以在我的 return_written_record_id
函数中执行所有其他数据库操作,但是是否可以 return 我的 post
函数的所有 ID?或者这就是它必须在龙卷风中使用协程的方式?
如有任何建议,我们将不胜感激。谢谢!
只需执行:
result = yield new_topic_record
每当您在协程中生成 Future 时,Tornado 都会暂停协程,直到 Future 被解析为值或异常。然后 Tornado 通过在 "yield" 表达式中传递值或引发异常来恢复协程。
有关详细信息,请参阅 Refactoring Tornado Coroutines。
我正在使用 RethinkDB
和 Tornado
以及异步方法。基于我在 RethinkDB
中的数据模型,在为我的 topic
table 插入一条记录后,我还必须 update/insert 新记录到 user_to_topic
table.这是我的 post 请求处理程序的基本设置。
class TopicHandler(tornado.web.RequestHandler):
def get(self):
pass
@gen.coroutine
def post(self, *args, **kwargs):
# to establish databse connection
connection = rth.connect(host='localhost', port=00000, db=DATABASE_NAME)
# Thread the connection
threaded_conn = yield connection
title = self.get_body_argument('topic_title')
# insert into table
new_topic_record = rth.table('Topic').insert({
'title': topic_title,
}, conflict="error",
durability="hard").run(threaded_conn)
# {TODO} for now assume the result is always written successfully
io_loop = ioloop.IOLoop.instance()
# I want to return my generated_keys here
io_loop.add_future(new_topic_record, self.return_written_record_id)
# do stuff with the generated_keys here, how do I get those keys
def return_written_record_id(self, f):
_result = f.result()
return _result['generated_keys']
插入操作完成后,具有 return 的 Future
对象来自 RethinkDB
通过 Future.result()
方法插入操作的结果,我可以使用 [=检索新记录的 ID =22=] 结果的属性。根据 Tornado
document,我可以在我的回调函数 return_written_record_id
中处理结果。当然,我可以在我的 return_written_record_id
函数中执行所有其他数据库操作,但是是否可以 return 我的 post
函数的所有 ID?或者这就是它必须在龙卷风中使用协程的方式?
如有任何建议,我们将不胜感激。谢谢!
只需执行:
result = yield new_topic_record
每当您在协程中生成 Future 时,Tornado 都会暂停协程,直到 Future 被解析为值或异常。然后 Tornado 通过在 "yield" 表达式中传递值或引发异常来恢复协程。
有关详细信息,请参阅 Refactoring Tornado Coroutines。