Mehraban
Mehraban

Reputation: 3324

How to develop lock mechanism based on operations?

I have 3 different operations: add, delete and get.

While there can be any number of get operations running in parallel, there can't be any operations parallel to add and delete ie., while adding new items there can't be any add, delete or get operations. This holds for delete operations too.

If I just use a lock object, parallel get operations can't be done. What is the right way to implement this lock mechanism?

Upvotes: 0

Views: 35

Answers (1)

UltraInstinct
UltraInstinct

Reputation: 44424

Looks like you want a "Reader-Writer" lock. Python doesn't have it by default (I think). You may want to google search external libraries.

This came up from a google search. I haven't used it, but from the docs, usage seems quite straightforward.

Or if you want to roll out your own implementation, you could potentially do it with threading.Condition while keeping track of number of readers.

Upvotes: 1

Related Questions