Exclusive file access between processes in Python

Note that I investigated how to control exclusive files between processes in Python. There is a lockfile in the documentation

This package is deprecated. It is highly preferred that instead of using this code base that instead fasteners or oslo.concurrency is used instead.

Since there is, try using fasteners. It's located at PyPI so it's easy to install with pip:

pip install fasteners

Of the Examples in the document, this time interprocess lock Try using it. You can use with or make it a decorator.

import os
import time
import fasteners

def main():
    pid = os.getpid()
    while True:
        print("Waiting...")
        with fasteners.InterProcessLock('/tmp/tmp_lock_file'):
            print('Locked by {}'.format(pid))
            time.sleep(2)
        time.sleep(1)

if __name__ == '__main__':
    main()

The part inside with is executed exclusively.

import os
import time
import fasteners

@fasteners.interprocess_locked("/tmp/tmp_lock_file")
def action_with_lock():
    pid = os.getpid()
    print("{} has a lock".format(pid))
    time.sleep(1)

if __name__ == '__main__':
    action_with_lock()

If you want to execute one function exclusively, here. Both are easy to implement. It is convenient because it is not necessary to create / tmp / tmp_lock_file in advance.

Recommended Posts

Exclusive file access between processes in Python
Exclusive control with lock file in Python
File operations in Python
File processing in Python
File operations in Python
Download the file in Python
File / folder path manipulation in Python
Difference between list () and [] in Python
Linebot creation & file sharing in Python
Create a binary file in Python
I learned about processes in Python
ORC, Parquet file operations in Python
Access the Twitter API in Python
File open function in Python3 (difference between open and codecs.open and speed comparison)
Differences in authenticity between Python and JavaScript
Differences between Ruby and Python in scope
difference between statements (statements) and expressions (expressions) in Python
Revived from "no internet access" in Python
Difference between @classmethod and @staticmethod in Python
Difference between append and + = in Python list
Difference between nonlocal and global in Python
Write O_SYNC file in C and Python
Read the file line by line in Python
Read the file line by line in Python
[GPS] Create a kml file in Python
Access S3 resources via Cognito in Python
Differences in multithreading between Python and Jython
[Python] Read the specified line in the file
Quadtree in Python --2
Python in optimization
CURL in python
Script python file
Metaprogramming in Python
Python 3.3 in Anaconda
SendKeys in Python
Create a GIF file using Pillow in Python
File DL, byte value and delete in Python3
Read a file containing garbled lines in Python
[python] Difference between variables and self. Variables in class
From file to graph drawing in Python. Elementary elementary
Epoch in Python
Discord in Python
Sudoku in Python
DCI in Python
Python file processing
quicksort in python
About the difference between "==" and "is" in python
nCr in python
[Python] Implemented automation in excel file copying work
N-Gram in Python
Programming in python
Constant in python
Transpose CSV file in Python Part 2: Performance measurement
Copy data between Google Keep accounts in Python
Pharmaceutical company researchers summarized file scanning in Python
Lifegame in Python.
FizzBuzz in Python
GUI (WxPython) executable file (pyInstaller) [Windows] in Python3
Sqlite in python
StepAIC in Python
N-gram in python