support for tailable cursor #15

joerussbowman opened this Issue Sep 15, 2011 · 0 comments

1 participant



So I finally got time to look at using tailable cursor with mongodb. I've been playing with asyncmongo a bit, planning on using it to read/write within requests. It however doesn't really work with tailable cursors because there needs to be a real cursor to iterate on, and asyncmongo doesn't seem to return those? I played around with the Cursor class, and it doesn't have support for attributes like alive and methods like next.

Here's how I set up a nonblocking tailable cursor implementation with Tornado and pymongo. Maybe something like this could be included in an asyncmongo at some point?

        self.msgs_db = pymongo.Connection(host='', 
        self.msgs_cursor = self.msgs_db.mydb.mycoll.find({}, tailable=True)


    def tail(self, count=0):
        if count > 100:
            IOLoop.instance().add_timeout(time.time() + .5, self.tail)
        if self.msgs_cursor.alive:
            except StopIteration:
                IOLoop.instance().add_timeout(time.time() + .5, self.tail)
            self.msgs_cursor = self.msgs_db.mydb.mycoll.find({}, tailable=True)

notes: mycoll needs to be a capped collection. The purpose is to be able to just stream updates to each app server as they happen, rather than repeatedly poll the database. The count is just in case messages are coming in too fast, grabbing x at a time (100 in this case, which is probably way too low) and then freeing the loop up seems to be a way to go.

Thinking about this, it might actually be better performance to just poll, and I think I might go in that direction after all. But I didn't want to just throw the code away if it's something you might be able to use and others might have a use for.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment