In a multiprocess program I want to lock certain function based on arguments e.g.
def calculate(spreadsheet):
_do_calc(spreadsheet)
Now what I want to do is based on spreadsheet, lock the function so that multiple spreadsheets can be worked on concurrently but two calls on same spreadsheet will lock e.g.
def calculate(spreadsheet):
with Lock(spreadsheet.id):
_do_calc(spreadsheet)
Is it possible with multiprocessing.Lock , if not what are the alternatives? I am thinking of using fcntl based file locking.
Edit: Above was a very simplified version of my problem, basically I am not using multiprocessing module, I have N number of different scripts which work on some data and sometimes same data which I wanted to avoid, so wanted someway to synchronize them at some points, e.g. all of them may import a module mylock and use mylock.lock to synchronize
import mylock
def calculate(spreadsheet):
with mylock.lock(spreadsheet.id):
_do_calc(spreadsheet)
What are the various ways i can write mylock.lock, currently I just create a file with name spreadsheet.id and delete it when lock releases.