【问题标题】:Tailable cursor in pymongo seems to have stopped workingpymongo 中的可尾光标似乎已停止工作
【发布时间】:2015-04-30 05:00:42
【问题描述】:

我已经成功地在 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

【问题讨论】:

    标签: python mongodb pymongo


    【解决方案1】:

    在 pymongo 3.0 中,find 为此采用了cursor_type 选项。 tailableawait_data 参数已被删除。

    应该是这样的:

    cursor = refreq.find(cursor_type = CursorType.TAILABLE_AWAIT)
    

    【讨论】:

    • 是的 - 只需从 pymongo 导入它,然后从 pymongo 导入 MongoClient、CursorType,它就会根据您的回答再次运行。
    猜你喜欢
    • 2017-12-15
    • 2012-06-07
    • 1970-01-01
    • 2013-12-01
    • 1970-01-01
    • 2010-12-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多