Hoping someone can help me understand if I see a problem, or if I just don't understand the behavior of the mongodb tail cursor. I am running mongodb 2.0.4 and pymongo 2.1.1.
Here is a script that demonstrates the problem.
import sys
import time
import pymongo
MONGO_SERVER = "127.0.0.1"
MONGO_DATABASE = "mdatabase"
MONGO_COLLECTION = "mcollection"
mongodb = pymongo.Connection(MONGO_SERVER, 27017)
database = mongodb[MONGO_DATABASE]
if MONGO_COLLECTION in database.collection_names():
database[MONGO_COLLECTION].drop()
print "creating capped collection"
database.create_collection(
MONGO_COLLECTION,
size=100000,
max=100,
capped=True
)
collection = database[MONGO_COLLECTION]
if len(sys.argv[1:]):
collection.insert(
{
"key" : "value",
}
)
cursor = collection.find( {},
await_data=True,
tailable=True )
try:
while cursor.alive:
print "Top of the loop"
try:
message = cursor.next()
print message
except StopIteration:
print "MongoDB, why you no block on read?!"
time.sleep(1)
except pymongo.errors.OperationFailure:
print "Delete the collection while running to see this."
except KeyboardInterrupt:
print "trl-C Ya!"
sys.exit(0)
print "and we're out"
So, if you look at the code, it’s pretty simple to demonstrate the problem I am facing. When I run the code against an empty collection (properly closed and ready for the tail), the cursor dies and my code exits after one loop. Adding the first entry to the collection makes it behave as I would expect if the tail cursor acted.
, StopIteration, .next(), ? , ? , await_data - , , , .
, While True cursor.alive, , script , , CPU. , .