2

Using sample code from 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)

Getting traceback:

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

The documentation says this should return a Future if no callback is passed, but it throws an exception instead. Using gen.Task does the work, but I don't understand why a straightforward example from the tutorial does not work.

4

1 回答 1

2

You used the "latest" tutorial with the "stable" code. Read the "stable" tutorial instead.

Background: Motor on PyPI is at version 0.1.2. Version 0.1.2 is the current "stable" version with a callback-based API. You can use it with gen.Task, as the "stable" tutorial demonstrates. As the tutorial will tell you, you should actually use motor.Op, which is like gen.Task with better exception semantics.

The "latest" tutorial you were reading reflects the extremely unstable code I have in Motor's master branch on GitHub. This will be released as Motor 0.2 within the next couple months and become the new "stable." Meanwhile, please follow the current "stable" documentation.

于 2013-12-11T18:13:48.950 回答