views:

46

answers:

2

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.

+1  A: 

Why not give each spreadsheet a Lock as an instance attribute?

class Spreadsheet(...):
    def __init__(self, ...):
        self.lock = multiprocessing.Lock()

    ...

and then

def calculate(spreadsheet):
    with spreadsheet.lock:
        ...
katrielalex
See the edit, I more fully explained my case.
Anurag Uniyal
A: 

You might be able to improve on simple file opening with this recipe, which I have tested on Linux and Windows.

Vinay Sajip
I am actually using fcntl.lockf which works pretty well, but wanted to see if there are any better solutions
Anurag Uniyal