Skip to content
This repository has been archived by the owner on Nov 5, 2019. It is now read-only.

support for tailable cursor #15

Open
joerussbowman opened this issue Sep 15, 2011 · 0 comments
Open

support for tailable cursor #15

joerussbowman opened this issue Sep 15, 2011 · 0 comments

Comments

@joerussbowman
Copy link

Hi,

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='127.0.0.1', 
                    port=27017)
        self.msgs_cursor = self.msgs_db.mydb.mycoll.find({}, tailable=True)
        self.msgs_cursor.batch_size(10)

        self.tail()

    def tail(self, count=0):
        if count > 100:
            IOLoop.instance().add_timeout(time.time() + .5, self.tail)
        if self.msgs_cursor.alive:
            try:
                logging.error(self.msgs_cursor.next())
                self.tail(count=count+1)
            except StopIteration:
                IOLoop.instance().add_timeout(time.time() + .5, self.tail)
        else:
            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 subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Development

No branches or pull requests

1 participant