1

我编写了以下简化版本的代码:

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 的错误。但我认为我应该明确关闭它,因为它是一个可尾游标。

我用谷歌搜索了它,但我没有运气。有什么建议么?

4

1 回答 1

1

这是 Motor 中的一个未知错误,我已在MOTOR-67跟踪并修复它。你观察到几个问题。

首先,Motor cursor 的析构函数有一个错误,它会尝试向 MongoDB 服务器发送“killcursors”消息,即使在您调用 close 之后也是如此。您关闭了光标,断开了客户端,并退出了 Python 解释器。在解释器关闭期间,游标被破坏并尝试将“killcursors”发送到服务器,但客户端断开连接,因此操作失败并记录警告。这是我已修复的错误,将在 Motor 0.6 中发布。

您从引用游标的函数中调用 exit(),因此游标的析构函数在解释器关闭期间运行。关机顺序复杂且不可预测;通常,析构函数在greenlet模块被销毁后运行。当游标析构函数在第 1798 行greenlet.getcurrent()调用时,该函数已设置为,因此“TypeError: 'NoneType' object is not callable”。getcurrentNone

我建议不要从函数中调用“exit()”。您的调用IOLoop.current().stop()允许start函数返回,解释器优雅地退出。

于 2016-03-05T15:37:47.333 回答