1 year ago

#285890

test-img

Ruthie

Is it possible to prevent creating multiple locks for thasame file using fcntl.flock() python?


I'm using fcntl.flock in python to lock files.
I warped the locking mechanism in class something like that:
class Locker:
    def __enter__ (self):
        self.fp = open("./lockfile.lck")
        fcntl.flock(self.fp.fileno(), fcntl.LOCK_EX)

    def __exit__ (self, _type, value, tb):
        fcntl.flock(self.fp.fileno(), fcntl.LOCK_UN)
        self.fp.close()

my problem is that I run the process from different file and import the class and it creates me 2 different locks for this file....
How can I solve that? might be it because I'm creating open("./lockfile.lck")???
Any help would be appreciated!

python

file-io

locks

0 Answers

Your Answer

Accepted video resources