Thomas Browne
Thomas Browne

Reputation: 24888

Tailable cursor in pymongo seems to have stopped working

I have successfully been using tailable cursors in Pymongo for 2 years without a hitch, but all of a sudden, today, my identical code is throwing an "unexpected keyword" error:

enter image description here

I upgraded to 3.0 Mongo a few weeks ago and it was still working fine, but perhaps a new pymongo version now does it differently as I just installed a new version (3.0.1) today? Previously it was pymongo 2.6.3. My code:

cursor = refreq.find(tailable = True, await_data = True)
while cursor.alive:
    xxx

So basically anytime something is inserted into refreq collection I want to know about it, without polling. Used to work fine. Pymongo version 3.0.1 recently installed (today).

Tried to put in an empty dict too

cursor = refreq.find({}, tailable = True, await_data = True)

but still gives the identical error. Has something changed?

Here is the full thread code, for reference:

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

Upvotes: 1

Views: 1761

Answers (1)

JohnnyHK
JohnnyHK

Reputation: 311905

In pymongo 3.0, find takes a cursor_type option for this. The tailable and await_data parameters have been removed.

So it should be:

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

Upvotes: 6

Related Questions