【问题标题】:How do I find out the cursor_id for kill_cursors()?如何找出 kill cursors() 的光标 ID?
【发布时间】:2013-12-07 16:57:11
【问题描述】:

PyMongo API 文档提到了一个杀死 mongodb 游标的函数:

kill_cursors(cursor_ids) Send a kill cursors message with the given ids.
Raises TypeError if cursor_ids is not an instance of list.
Parameters :     cursor_ids: list of cursor ids to kill

但是,它没有指定(据我所知)如何实际找出光标的 id。我怎样才能得到这个?

from pymongo import Connection
c = Connection()
crawl = c.db.coll.find()
print crawl.id? <-- no idea how to get this

【问题讨论】:

  • 一旦游标被执行,它就在 cursor._Cursor__id 中(在游标执行前会被设置为 0)

标签: python mongodb cursor pymongo


【解决方案1】:

Jesse,如果我发现它是一个流氓游标,或者在测试期间,我可能仍然想明确地杀死它。在 java 脚本管理控制台中拥有该选项将是一个加分项。除此之外,我们是否有办法列出 mongo 服务器上所有打开的游标?

【讨论】:

  • 是的。这是一个后续问题。我们有没有办法在 mongo 中打印所有打开的光标 ID?
  • 所以请打开一个新问题。谢谢。
【解决方案2】:

您永远不需要显式地终止游标。一旦您从游标中检索到所有结果,服务器就会自动清理游标。 (例如,使用for doc in collection.find() 循环。)如果您没有检索所有结果,PyMongo 会在其 Cursor 实例被取消引用时自动终止游标。例如: for doc in collection.find(): assert False # raise exception 当游标超出范围时,PyMongo 会自动调用 kill_cursors 清理游标服务器端。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-09-30
    • 2016-01-30
    • 2022-01-17
    • 2019-05-17
    • 1970-01-01
    • 2015-12-20
    • 2016-01-08
    相关资源
    最近更新 更多