Package: lockfolder
Lock FolderObtain a lock by posting a bid into a lock folder, and checking for rivals.Installationpip install lockfolderHow to Use Itimport pathlib
from lockfolder import lockfolder

lockfolder.init()

p = pathlib.Path("path/to/lockfolder")

if lockfolder.lock(p):
    print("aquired lock")
    ...
    ...
    lockfolder.unlock(p)
else:
    print("failed to aquire lock")Adapt to context manager, throw-finally systems, decorator, what have
you, as you like.Lock FilesLock files are JSON files that are logically equivalent to:----------[ filename: <GUID>.json ]-----------------------------------
  {
    "PID": <integer PID>,
    "CREATED": <integer timestamp>
  }
----------------------------------------------------------------------ConceptThis is a mutex system, that ensures that only one process has access to a resource.It's defining features are:a filesystem folder is used to bid for a lockit is cross-platform to contemporary operating systemsit's very simpleit has only one external dependency:psutilit is robust to recycled PIDsSome of the limitations of this system, are:live-locks are possible, which occurs when too many processes all try to get a lock at the same timeall petitioners will fail to achieve a lockinsistent petitioners should repeat efforts at connection with an exponential backoffsecurity and file permissions are not taken account ofit is possible for a malicious user or process to manually delete or create lock filesthere is an extremely unlikely possibility of a v4 GUID collisionPID recycling fails if the same PID is recycled to a competitor within the second (highly unusual)Basic Strategy:each process self-assigns a GUIDeach process notes its PIDeach process notes its create-timea folder keeps the locks for the resource ("bids" and "locks" are the same thing in this system)the procedure is "check-bid-check", and if any check shows another bid/lock, the process deletes its own bid (if it got that far) and returns "fail"if the "check-bid-check" passes, then the bid is left until the process completes, at which point the bid is deletedbids contain the process PID and creation time of the process, and may be deleted by any process that wishes to challenge a prior bid, provided that it can determine that a process created at  with process ID  is no longer runningupon a fail, at the programmer's discretion, processes may delay and retry, with delays having a random component, and increasing duration between attemptsProcedure:Here is the basic procedure more specifically:STEP 10.CHECK-- check the lock folder for the presence of any files; if there are files, END (error state: FAIL); if there are no files, proceed to step 20STEP 20.BID-- write a file into the lock folder, containing the following, then proceed to step 30filename:(self-selected-GUID-for-this-process).jsonJSON file content:PID:(process-id)PROCESS-START-TIME:(timestamp-for-this-process)STEP 30.CHECK-- check the lock folder for the presence of any files, other than my own: if there are other files, proceed to step 40, otherwise, proceed to step 50STEP 40.DELETE & FAIL-- delete the bid file that was created in step 20, then END (error state: FAIL)STEP 50.OPERATE-- the lock has been acquired (it's the same file as the bid created in step 20) -- do whatever you please with itSTEP 60.DELETE & END-- delete the bid file that was created in step 20, then END (error state: SUCCESS)Additional Resourceshttps://www.reddit.com/r/AskProgramming/comments/186ot93/is_this_checkbidcheck_mutex_strategy_okay/-- a Reddit thread, in which I requested verification of correctness for this system.written: 2023-11-29