我编写了以下代码的简化版本:
from sys import exit
from tornado.ioloop import IOLoop
from tornado.gen import coroutine
from pymongo.errors import CollectionInvalid
from motor import MotorClient
client = MotorClient()
db = client.db_test
coll_name = 'coll_test'
coll = db[coll_name]
cursor = None
@coroutine
def stop():
yield cursor.close()
client.disconnect()
IOLoop.current().stop()
exit()
@coroutine
def create_cursor():
global cursor
try:
yield db.create_collection(coll_name, capped=True, size=1000000)
except CollectionInvalid:
print('Database alredy exists!')
yield coll.save({})
yield coll.save({})
cursor = coll.find(tailable=True, await_data=True)
yield cursor.fetch_next
cursor.next_object()
if __name__ == "__main__":
IOLoop.current().spawn_callback(create_cursor)
IOLoop.current().call_later(10, stop)
IOLoop.current().start()
当我运行它时,我随机地得到这两个错误中的一个或一个:
Exception ignored in: <bound method MotorCursor.__del__ of MotorCursor(<pymongo.cursor.Cursor object at 0x7fd3a31e5400>)>
Traceback (most recent call last):
File "./env/lib/python3.4/site-packages/motor/__init__.py", line 1798, in __del__
TypeError: 'NoneType' object is not callable
Exception ignored in: <bound method MotorCursor.__del__ of MotorCursor(<pymongo.cursor.Cursor object at 0x7f4bea529c50>)>
Traceback (most recent call last):
File "./env/lib/python3.4/site-packages/motor/__init__.py", line 1803, in __del__
File "./env/lib/python3.4/site-packages/motor/__init__.py", line 631, in wrapper
File "./env/lib/python3.4/site-packages/tornado/gen.py", line 204, in wrapper
TypeError: isinstance() arg 2 must be a type or tuple of types
我使用的是Python 3.4.3,Tornado 4.1,Pymongo 2.8,Motor 0.4.1和MongoDB 2.6.3。
仅在光标创建时tailable
和await_data
选项为True
时才会出现此问题。
当我不关闭光标时,我也会收到Pymongo的错误。但我认为我应该明确地关闭它,因为它是一个可用的游标。
我用谷歌搜索了它,但我没有运气。有什么建议吗?
答案 0 :(得分:1)
这是Motor中的一个未知错误,我已经跟踪并修复了MOTOR-67。你发现了几个问题。
首先,Motor游标的析构函数有一个错误,它会尝试发送&#34; killcursors&#34;即使在您关闭之后,也会向MongoDB服务器发送消息。您关闭了光标,断开了客户端,并退出了Python解释器。在解释器关闭期间,光标被销毁并尝试发送&#34; killcursors&#34;到服务器,但客户端已断开连接,因此操作失败并记录警告。这是我已修复并将在Motor 0.6中发布的错误。
从具有对游标的引用的函数内调用exit(),因此游标的析构函数在解释器关闭期间运行。关机顺序复杂且不可预测;通常,析构函数在greenlet
模块被销毁后运行。当光标析构函数在line 1798调用greenlet.getcurrent()
时,getcurrent
函数已设置为None
,因此&#34; TypeError:&#39; NoneType&#39;对象不可调用&#34;。
我建议不要拨打&#34;退出()&#34;来自一个功能。您对IOLoop.current().stop()
的调用允许start
函数返回,并且解释器可以正常退出。