I have a MongoDB collection (used as a job queue) from which several processes read records using findAndModify . findAndModify searches for records in which the active field is false, setting it to true so that other processes do not read the same record.
The problem is that looking at the logs, I see that different processes are still reading the same notes. This occurs when two processes are read from the queue at the same time. Is there a way to make sure that only one process is read from a collection at a time?
I am using Mongo 2.2.3 and pymongo 2.2.
Thank you very much!
EDIT: The magazines in question are:
worker.3 2013-03-18 23: 57: 45,434 default-worker-3
project_name INFO Queue task: job ID: 5147a90f68e8fe0097002bdf
worker.3 2013-03-18 23: 57: 47 608 default-worker-3
project_name INFO Login: 14497 documents
worker.2 2013-03-18 23: 57: 45 440 default-worker-2
project_name INFO Queue task: job ID: 5147a90f68e8fe0097002bdf
worker.2 2013-03-18 23: 57: 47,658 default-worker-2
project_name INFO Login: 14497 documents
As you can see, worker.3 and worker.2 are reading the same job from the queue (it has the same mongodb id for both workers).
Find_and_modify command:
query = {"active": False} try: return self.collection.find_and_modify( query=query, update={"$set": {"active": True}}, upsert=False, sort={"added_on": 1}, limit=1 ) except Exception, exc: LOGGER.exception(exc)
source share