2013-12-10 36 views
2

使用來自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 

文檔說,如果沒有回調被傳遞,應返回一個未來,但它拋出一個異常來代替。使用gen.Task來完成這項工作,但我不明白爲什麼來自教程的直接示例不起作用。

回答

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 0.2發佈,併成爲新的「穩定版」。同時,請按照當前的「穩定」文檔。

+0

後續:[電機0.2發佈](http://emptysqua.re/blog/motor-0-2-released/)於2014年4月,**現在** [[穩定]文檔](http ://motor.readthedocs.org/en/stable/tutorial.html)是0.2。 –