georg
georg

Reputation: 215009

Killing a query before any data arrives

I've got a fairly large collection and make non-indexed (intentionally) queries that may take a long time to return the first batch of results, or to indicate failure. I want these queries to fail if nothing gets returned in say 10 seconds. So I wrote this:

try:
     cur = big_collection.find(query, network_timeout=10)
     for doc in cur:
          print doc
except pymongo.errors.AutoReconnect:
     print "failure!"

This works fine on the client, however, current_op says that the query continues executing on the server even after the timeout. I tried cur.close() and db.kill_cursors in the except block, but that didn't work, probably because cur.cursor_id remains None until it receives any data from the server.

Is there a way to kill a query even if it hasn't returned any data yet?

Upvotes: 1

Views: 303

Answers (1)

Leftium
Leftium

Reputation: 17903

There are no PyMongo wrappers, but you can use the related database commands to query for the opid and kill the operation. It will look something like: db['$cmd.sys.killop'].find_one({op:op}). For some reason, this doesn't seem to work with PyMongo 2.1.

See:

Upvotes: 2

Related Questions