Adding a read/write lock on a collection using PyMongo

283 views
Skip to first unread message

Alex Dunn

unread,
Apr 9, 2017, 3:05:26 AM4/9/17
to mongodb-user
Hi,

I need to limit read and write access of a particular collection to a single process. Basically what I have is a group of concurrent processes each doing:

>>> Read something from collection
>>> Process data
>>> Write something to collection

I can only have one process executing the above workflow at a time - ie the processes must access the collection sequentially (the rest of the db should remain unlocked). Is there a way in MongoDB/WiredTiger to lock collection reads and writes? I have tried instantiating a MongoClient object with maxPoolSize set to 1, but I think that only works for threads in a single process. 

Any help is appreciated!

Kevin Adistambha

unread,
Apr 13, 2017, 2:31:38 AM4/13/17
to mongodb-user

Hi Alex,

I need to limit read and write access of a particular collection to a single process. … Is there a way in MongoDB/WiredTiger to lock collection reads and writes?

Currently there is no method in MongoDB that will “lock” a collection, since the WiredTiger storage engine was designed to allow more concurrency and not less.

My understanding is, you have multiple application threads and you want to limit processing to only one thread at a time. If your use case permits it, you may be able to implement the locking mechanism using something like a REST interface in front of the database server, which could implement the locking behaviour you require (e.g. using a mutex or a queue).

Best regards,
Kevin

Reply all
Reply to author
Forward
0 new messages