我已经成功地在 Pymongo 中使用可尾游标 2 年了,没有任何障碍,但是突然之间,今天,我的相同代码抛出了一个“意外的关键字”错误:
几周前我升级到 3.0 Mongo,它仍然工作正常,但也许新的 pymongo 版本现在会有所不同,因为我今天刚安装了一个新版本 (3.0.1)?以前是pymongo 2.6.3。我的代码:
cursor = refreq.find(tailable = True, await_data = True)
while cursor.alive:
xxx
所以基本上任何时候我都想知道有什么东西被插入到 refreq 集合中,而不需要轮询。过去工作正常。最近安装了 Pymongo 3.0.1 版(今天)。
也尝试输入一个空的字典
cursor = refreq.find({}, tailable = True, await_data = True)
但仍然给出相同的错误。有什么变化吗?
完整的线程代码,供引用:
def handleRefRequests(db, refqueue):
""" handles reference data requests. It's threaded. Needs the database id.
will create a capped collection and constantly poll it for new requests"""
print("Dropping the reference requests collection")
db.drop_collection("bbrefrequests")
print("Recreating the reference requests collection")
db.create_collection("bbrefrequests", capped = True, size = 100 * 1000000) # x * megabytes
refreq = db.bbrefrequests
# insert a dummy record otherwise exits immediately
refreq.insert({"tickers":"test", "fields":"test", "overfields":"test", "overvalues":"test", "done": False})
cursor = refreq.find({}, tailable = True, await_data = True)
while cursor.alive:
try:
post = cursor.next()
if ("tickers" in post) and ("fields" in post) and ("done" in post): # making sure request is well formed
if post["tickers"] != "test":
refqueue.put(post)
except StopIteration:
time.sleep(0.1)
if not runThreads:
print("Exiting handleRefRequests thread")
break
请您参考如下方法:
在 pymongo 3.0 中,find为此需要一个 cursor_type 选项。 tailable 和 await_data 参数已被删除。
所以应该是:
cursor = refreq.find(cursor_type = CursorType.TAILABLE_AWAIT)
