module 'tornado.gen' has no attribute 'engine'
时间: 2023-10-04 10:11:56 浏览: 50
这个错误通常是因为使用了不兼容的 Tornado 版本导致的。在旧版本的 Tornado 中,确实存在 `tornado.gen.engine` 属性,但在新版本的 Tornado 中已经被移除了。
如果你的代码中使用了 `tornado.gen.engine`,那么你需要将其替换为 `tornado.gen.convert_yielded` 或 `tornado.gen.maybe_future`,这两个方法可以完成相同的任务。
如果你使用的是较新的 Tornado 版本,但仍然遇到了这个错误,那么你可以尝试重新安装 Tornado,或者查看你的 Python 环境中是否存在旧版本的 Tornado。你可以使用 `pip freeze` 命令查看所有已安装的 Python 包及其版本信息。如果存在旧版本的 Tornado,可以使用 `pip uninstall tornado` 命令卸载它,然后重新安装最新版本的 Tornado。
相关问题
AttributeError: module 'tornado.web' has no attribute 'asynchronous'
在Python中,当你使用`tornado.web`模块时,可能会遇到`AttributeError: module 'tornado.web' has no attribute 'asynchronous'`的错误。这个错误通常是因为`tornado`版本的问题导致的。在较新的`tornado`版本中,`asynchronous`已经被弃用,取而代之的是使用`asynchronous`装饰器的异步处理方式。
要解决这个问题,你可以尝试以下两种方法:
1. 更新`tornado`版本:如果你的`tornado`版本较旧,可以尝试更新到最新版本。你可以使用以下命令来更新`tornado`:
```shell
pip install --upgrade tornado
```
更新完成后,重新运行你的代码,看看问题是否解决了。
2. 使用`@tornado.gen.coroutine`装饰器:如果你无法更新`tornado`版本,你可以尝试使用`@tornado.gen.coroutine`装饰器来替代`asynchronous`。你可以按照以下步骤进行修改:
- 导入`@tornado.gen.coroutine`装饰器:
```python
from tornado.gen import coroutine
```
- 将原来的`asynchronous`装饰器替换为`@coroutine`装饰器:
```python
@coroutine
def your_handler(self):
# your code here
```
通过这种方式,你可以在较旧的`tornado`版本中使用异步处理。
这些方法应该能够解决`AttributeError: module 'tornado.web' has no attribute 'asynchronous'`的问题。请根据你的具体情况选择适合的方法进行尝试。
module 'tornado.web' has no attribute 'asynchronous'
这个错误通常是由于 Tornado 版本更新导致的,asynchronous 被废弃了。可以使用 `gen.coroutine` 或 `gen.asynchronous` 来代替。
示例:
```python
import tornado.web
from tornado import gen
class MyHandler(tornado.web.RequestHandler):
@gen.coroutine
def get(self):
yield gen.sleep(1)
self.write("Hello, world")
```
或者:
```python
import tornado.web
from tornado import gen
class MyHandler(tornado.web.RequestHandler):
@gen.asynchronous
@gen.engine
def get(self):
yield gen.Task(self.do_something_async)
self.write("Hello, world")
@gen.coroutine
def do_something_async(self):
yield gen.sleep(1)
# do some async work here
```
注意,`@gen.coroutine` 和 `@gen.asynchronous` 可以同时使用,但是不建议这样做。