python-3.x - 龙卷风环境中@cyclone.web.asynchronous 的替代品
问题描述
我们现在正在将我们的代码从旋风转移到龙卷风。以前我们使用@cyclone.web.asynchronous 作为我们的 api 之一,用于在 cyclone 中进行非阻塞异步调用(这样我们就不会阻塞 UI)。在 tornado 中有什么替代方法,@tornado.web.asynchronous 在 tornado 6.1 中不起作用。我的旋风代码是这样的
class ABCHandler(cyclone.web.RequestHandler):
@cyclone.web.asynchronous
def post(self):
some_validation()
# Spawn a thread to import the files and leave the post method
# asynchronous decorator will keep the request open to write the response on,
# once the import is complete
file_handler.start() ---- this is a thread that do all the heavy work and in this method we are
closing the request with self.finish
Class file_handler():
run(self):
{
---do some heavy work, like importing a file
self.importing_a_large_file()
self.set_status(status)
self.write(json_response)
self.finish()
}
它的龙卷风等效方法是什么。
我尝试了各种方法,例如添加 gencouroutine 装饰器,将方法名称更改为异步,但似乎没有任何效果。
解决方案
听起来cyclone.web.asynchronous
等同于tornado.web.asynchronous
,因此您最好先从 cyclone 迁移到 Tornado 5.1(其中asynchronous
仍然支持装饰器),然后在单独的步骤中迁移到协程和 Tornado 6.x。(或者如果 cyclone 支持协程,则在切换到 Tornado 之前先转到 cyclone 上的协程)。
如果您尝试cyclone.web.asynchronous
一次性使用原生协程从 Tornado 6 迁移到 Tornado 6,这将是一次非常困难的重构。此外,您的示例代码看起来像是RequestHandler.finish
从另一个线程调用方法。我不确定旋风是否允许这样做,但绝对不是龙卷风。
推荐阅读
- java - Spring Bean 配置总是返回 null
- arrays - 使用角度 5 转换动态值
- swift - 动画多个原型表视图单元格
- python - 报告实验室的画布对象的正确构造方法是什么?
- ruby-on-rails - 在 Windows 上运行的 Ubuntu bash 上设置 PostgreSQL
- javascript - 赛普拉斯调用不执行隐藏元素交互的“显示”方法
- csv - 使用 p:dataexporter 导出 csv
- r - 计算df中每个元素的百分比值(基于最大值);但是在指定的列中
- java - 每次我必须改变一些东西来反映
- android - 使用 Flutter/Dart 构建的应用空间