Motor Tailable Cursor ExampleΒΆ

A cursor on a capped collection can be tailed using fetch_next:

@gen.coroutine
def tail_example():
    results = []
    collection = db.my_capped_collection
    cursor = collection.find(tailable=True, await_data=True)
    while True:
        if not cursor.alive:
            # While collection is empty, tailable cursor dies immediately
            yield gen.Task(loop.add_timeout, datetime.timedelta(seconds=1))
            cursor = collection.find(tailable=True, await_data=True)

        if (yield cursor.fetch_next):
            results.append(cursor.next_object())
            print results

See also

Tailable cursors

Previous topic

Motor GridFS Examples

Next topic

Authentication With Motor

This Page