开发者

Multiprocess conditional/named Lock

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.


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


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:
        ...
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜