使用pymongo tailable游标在空集合上死亡

时间:2012-04-25 17:58:23

标签: python mongodb pymongo

希望有人可以帮助我理解我是否看到了问题,或者我是否只是不了解mongodb tailable游标行为。我正在运行mongodb 2.0.4和pymongo 2.1.1。

这是一个演示此问题的脚本。

#!/usr/bin/python

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]

# Run this script with any parameter to add one record
# to the empty collection and see the code below
# loop correctly
#
if len(sys.argv[1:]):
  collection.insert(
    {
      "key" : "value",
    }
  )

# Get a tailable cursor for our looping fun
cursor = collection.find( {},
                          await_data=True,
                          tailable=True )

# This will catch ctrl-c and the error thrown if
# the collection is deleted while this script is
# running.
try:

  # The cursor should remain alive, but if there
  # is nothing in the collection, it dies after the
  # first loop. Adding a single record will
  # keep the cursor alive forever as I expected.
  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"

# End

因此,如果您查看代码,那么演示我遇到的问题就非常简单了。当我针对一个空集合运行代码(正确上限并准备好进行拖尾)时,光标会死,并且我的代码在一个循环后退出。在集合中添加第一条记录使其行为与我期望拖尾光标的行为方式相同。

另外,处理等待数据的cursor.next()的StopIteration异常处理是什么?为什么在数据可用之前后端才会阻塞?我假设await_data实际上会做一些事情,但它似乎只能使连接等待一两秒,而不是没有它。

网络上的大多数示例都在cursor.alive循环周围放置了第二个While True循环,但是当脚本尾部收集空集合时,循环只是旋转并且旋转浪费CPU时间。我真的不想在应用程序启动时放入一个假记录,以避免这个问题。

1 个答案:

答案 0 :(得分:1)

这是已知行为,2循环“解决方案”是解决此案例的公认惯例。如果集合是空的,而不是像你建议的那样立即重试并进入紧密循环,你可以睡一小段时间(特别是如果你希望很快会有数据到尾)。