Prohibit multiple launches in python

fcntl ---fcntl and ioctl system calls — Python 3 \ .9 \ .1 documentation

lock_test.py


import os
import fcntl
import time

def process_lock():
    lockfile = os.path.splitext(os.path.abspath(__file__))[0] + '.lock'
    lockfp = open(lockfile, "w")
    try:
        fcntl.flock(lockfp, fcntl.LOCK_EX | fcntl.LOCK_NB)
    except IOError:
        return
    return lockfp

def main():
    lock = process_lock()
    if not lock:
        print("lock error")
        exit(1)

    #processing
    time.sleep(360)

if __name__ == '__main__':
    main()
$ python3 lock_test.py &
$ python3 lock_test.py
lock error

Recommended Posts

Prohibit multiple launches in python
Avoid multiple loops in Python
Extract multiple list duplicates in Python
[Python] Show multiple windows in Tkinter
Statistical test (multiple test) in Python: scikit_posthocs
Delete multiple elements in python list
Handle multiple python versions in one jupyter
When specifying multiple keys in python sort
Quadtree in Python --2
CURL in python
Metaprogramming in Python
Python 3.3 in Anaconda
Geocoding in python
SendKeys in Python
Meta-analysis in Python
Unittest in python
Send email to multiple recipients in Python (Python 3)
Discord in Python
DCI in Python
quicksort in python
nCr in python
N-Gram in Python
Programming in python
Process multiple lists with for in Python
Plink in Python
Constant in python
Lifegame in Python.
Sqlite in python
StepAIC in Python
N-gram in python
LINE-Bot [0] in Python
Csv in python
Disassemble in Python
Reflection in Python
Constant in python
nCr in Python.
format in python
Scons in Python3
Puyo Puyo in python
python in virtualenv
PPAP in Python
Reflection in Python
Chemistry in Python
Hashable in python
DirectLiNGAM in Python
LiNGAM in Python
Flatten in python
flatten in python
Get multiple maximum keys in Python dictionary type
Multiple graphs are displayed in one window (python)
[Python] Dealing with multiple call errors in ray.init
Sorted list in Python
Daily AtCoder # 36 in Python
Clustering text in Python
Daily AtCoder # 2 in Python
Implement Enigma in python
Daily AtCoder # 32 in Python
Daily AtCoder # 6 in Python
Edit fonts in Python
Singleton pattern in Python
File operations in Python