How to lock a directory between python processes on Linux?

I have two (or more) python processes and you want to create a concept similar to mutex exception for a shared resource. The "Share" in this case is a directory. How can I easily / standardly / etc implement a mutex? A hidden file .lockthat each process agrees to check and, if it exists, adds its PID as a new line and then issues its PID when they have access to the file?

I just want to clean the directory and make sure that no other process is trying to read or write it, while I clean it.

Is there a standard linux way? Maybe something I can just do with a shell string from python?

+4
source share
1 answer

Linux

On Linux, there are two standard types of locking: advisory locking (specified in POSIX) and mandatory locking (for Linux).

However, both of them can be applied only to files, but not to directories. So yes, you need a lock file. It is assumed that all users should be aware of the locked file and obtain a lock before accessing the directory. Therefore, a mandatory lock will not help here, and you need an advisory lock.

There are three types of file locks on Linux:

  • flock(2) (indicated in POSIX);
  • POSIX write lock, see the section "Secret Record of Reports" in fcntl(2), as well as lockf(3)wrapper (both specified in POSIX);
  • , . fcntl(2) ( Linux, ).

Python

Python, flock(), lockf() fcntl() fcntl. flock , - fcntl.flock.

:

import flock

with open('/my/dir/lockfile', 'w') as fp:
    with flock.Flock(fp, flock.LOCK_EX) as lock:
        pass # exclusive lock is acquired here

PS.

, . , , FUSE, , .

+3

Source: https://habr.com/ru/post/1607522/


All Articles