使用motor tutorial中的示例代码。
from tornado import gen
db = motor.MotorClient('localhost', 1235).open_sync().packmon
@gen.coroutine
def do_find():
cursor = db.test_collection.find()
for document in (yield cursor.to_list(length=100)):
print document
tornado.ioloop.IOLoop.current().run_sync(do_find)
获取追溯:
Traceback (most recent call last):
File "app_main.py", line 51, in run_toplevel
File "chat.py", line 22, in <module>
tornado.ioloop.IOLoop.current().run_sync(do_find)
File "/home/user/venv/packmon-pypy/site-packages/tornado/ioloop.py", line 370, in run_sync
return future_cell[0].result()
File "/home/user/venv/packmon-pypy/site-packages/tornado/concurrent.py", line 129, in result
raise_exc_info(self.__exc_info)
File "/home/user/venv/packmon-pypy/site-packages/tornado/gen.py", line 221, in wrapper
runner.run()
File "/home/user/venv/packmon-pypy/site-packages/tornado/gen.py", line 507, in run
yielded = self.gen.send(next)
File "chat.py", line 19, in do_find
for document in (yield cursor.to_list(length=100)):
File "/home/user/venv/packmon-pypy/site-packages/motor/__init__.py", line 1465, in to_list
check_callable(callback, required=True)
File "/home/user/venv/packmon-pypy/site-packages/motor/__init__.py", line 74, in check_callable
raise TypeError("callback is required")
TypeError: callback is required
文档说如果没有传递回调,它应返回Future,但它会抛出异常。使用gen.Task完成工作,但我不明白为什么教程中的简单示例不起作用。
答案 0 :(得分:2)
您使用了“最新”教程和“稳定”代码。 Read the "stable" tutorial instead.
背景:Motor on PyPI is at version 0.1.2。版本0.1.2是当前具有基于回调的API的“稳定”版本。正如“稳定”教程所示,您可以将其与gen.Task
一起使用。正如本教程将告诉您的那样,您应该使用motor.Op
,这与gen.Task
具有更好的异常语义。
您正在阅读的“最新”教程反映了我在GitHub上的Motor主分支中的极不稳定的代码。这将在未来几个月内作为Motor 0.2发布,并成为新的“稳定”。同时,请遵循当前的“稳定”文档。