filelock - Man Page

Name

filelock — filelock 3.8.2

This package contains a single module, which implements a platform independent file lock in Python, which provides a simple way of inter-process communication:

from filelock import Timeout, FileLock

lock = FileLock("high_ground.txt.lock")
with lock:
    with open("high_ground.txt", "a") as f:
        f.write("You were the chosen one.")

Don't use a FileLock to lock the file you want to write to, instead create a separate .lock file as shown above. [image: Example gif] [image]

Similar Libraries

Perhaps you are looking for something like:

Installation

filelock is available via PyPI, so you can pip install it:

python -m pip install filelock

Tutorial

A FileLock is used to indicate another process of your application that a resource or working directory is currently used. To do so, create a FileLock first:

from filelock import Timeout, FileLock

file_path = "high_ground.txt"
lock_path = "high_ground.txt.lock"

lock = FileLock(lock_path, timeout=1)

The lock object supports multiple ways for acquiring the lock, including the ones used to acquire standard Python thread locks:

with lock:
    with open(file_path, "a") as f:
        f.write("Hello there!")

lock.acquire()
try:
    with open(file_path, "a") as f:
        f.write("General Kenobi!")
finally:
    lock.release()


@lock
def decorated():
    print("You're a decorated Jedi!")


decorated()

The acquire method accepts also a timeout parameter. If the lock cannot be acquired within timeout seconds, a Timeout exception is raised:

try:
    with lock.acquire(timeout=10):
        with open(file_path, "a") as f:
            f.write("I have a bad feeling about this.")
except Timeout:
    print("Another instance of this application currently holds the lock.")

The lock objects are recursive locks, which means that once acquired, they will not block on successive lock requests:

def cite1():
    with lock:
        with open(file_path, "a") as f:
            f.write("I hate it when he does that.")


def cite2():
    with lock:
        with open(file_path, "a") as f:
            f.write("You don't want to sell me death sticks.")


# The lock is acquired here.
with lock:
    cite1()
    cite2()
# And released here.

Logging

All log messages by this library are made using the DEBUG_ level, under the filelock name. On how to control displaying/hiding that please consult the logging documentation of the standard library. E.g. to hide these messages you can use:

logging.getLogger("filelock").setLevel(logging.INFO)

Filelock VS Softfilelock

The FileLock is platform dependent while the SoftFileLock is not. Use the FileLock if all instances of your application are running on the same platform and a SoftFileLock otherwise.

The SoftFileLock only watches the existence of the lock file. This makes it ultra portable, but also more prone to dead locks if the application crashes. You can simply delete the lock file in such cases.

Asyncio Support

This library currently does not support asyncio. We'd recommend adding an asyncio variant though if someone can make a pull request for it, see here.

Contributions and Issues

Contributions are always welcome, please make sure they pass all tests before creating a pull request. This module is hosted on GitHub. If you have any questions or suggestions, don't hesitate to open a new issue 😊. There's no bad question, just a missed opportunity to learn more.

API

A platform independent file lock that supports the with-statement.

filelock.__version__

version of the project as a string

filelock.FileLock

alias of UnixFileLock

class filelock.SoftFileLock(lock_file, timeout=-1)

Bases: BaseFileLock

Simply watches the existence of the lock file.

exception filelock.Timeout(lock_file)

Bases: TimeoutError

Raised when the lock could not be acquired in timeout seconds.

lock_file

The path of the file lock.

class filelock.UnixFileLock(lock_file, timeout=-1)

Bases: BaseFileLock

Uses the fcntl.flock() to hard lock the lock file on unix systems.

class filelock.WindowsFileLock(lock_file, timeout=-1)

Bases: BaseFileLock

Uses the msvcrt.locking() function to hard lock the lock file on windows systems.

class filelock.BaseFileLock(lock_file, timeout=-1)

Bases: ABC, ContextDecorator

Abstract base class for a file lock object.

property lock_file
Return type

str

Returns

path to the lock file

property timeout
Return type

float

Returns

the default timeout value, in seconds

New in version 2.0.0.

property is_locked
Return type

bool

Returns

A boolean indicating if the lock file is holding the lock currently.

Changed in version 2.0.0: This was previously a method and is now a property.

acquire(timeout=None, poll_interval=0.05, *, poll_intervall=None, blocking=True)

Try to acquire the file lock.

Parameters
  • timeout (float | None) -- maximum wait time for acquiring the lock, None means use the default timeout is and if timeout < 0, there is no timeout and this method will block until the lock could be acquired
  • poll_interval (float) -- interval of trying to acquire the lock file
  • poll_intervall (float | None) -- deprecated, kept for backwards compatibility, use poll_interval instead
  • blocking (bool) -- defaults to True. If False, function will return immediately if it cannot obtain a lock on the first attempt. Otherwise this method will block until the timeout expires or the lock is acquired.
Raises

Timeout -- if fails to acquire lock within the timeout period

Return type

filelock._api.AcquireReturnProxy

Returns

a context object that will unlock the file when the context is exited

# You can use this method in the context manager (recommended)
with lock.acquire():
    pass

# Or use an equivalent try-finally construct:
lock.acquire()
try:
    pass
finally:
    lock.release()

Changed in version 2.0.0: This method returns now a proxy object instead of self, so that it can be used in a with statement without side effects.

release(force=False)

Releases the file lock. Please note, that the lock is only completely released, if the lock counter is 0. Also note, that the lock file itself is not automatically deleted.

Parameters

force (bool) -- If true, the lock counter is ignored and the lock is released in every case/

Return type

None

class filelock.AcquireReturnProxy(lock)

Bases: object

A context aware object that will release the lock file when exiting.

License

py-filelock is public domain:

This is free and unencumbered software released into the public domain.

Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.

In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.

For more information, please refer to <http://unlicense.org>

Changelog

v3.8.1 (2022-12-04)

  • Fix mypy does not accept filelock.FileLock as a valid type

v3.8.0 (2022-12-04)

  • Bump project dependencies
  • Add timeout unit to docstrings
  • Support 3.11

v3.7.1 (2022-05-31)

  • Make the readme documentation point to the index page

v3.7.0 (2022-05-13)

  • Add ability to return immediately when a lock cannot be obtained

v3.6.0 (2022-02-17)

  • Fix pylint warning "Abstract class WindowsFileLock with abstract methods instantiated" PR #135 - by @vonschultz
  • Fix pylint warning "Abstract class UnixFileLock with abstract methods instantiated" PR #135 - by @vonschultz

v3.5.1 (2022-02-16)

  • Use time.monotonic instead of time.time for calculating timeouts.

v3.5.0 (2022-02-15)

  • Enable use as context decorator

v3.4.2 (2021-12-16)

  • Drop support for python 3.6

v3.4.1 (2021-12-16)

  • Add stacklevel to deprecation warnings for argument name change

v3.4.0 (2021-11-16)

  • Add correct spelling of poll interval parameter for acquire method, raise deprecation warning when using the misspelled form PR #119 - by @XuehaiPan.

v3.3.2 (2021-10-29)

  • Accept path types (like pathlib.Path and pathlib.PurePath) in the constructor for FileLock objects.

v3.3.1 (2021-10-15)

  • Add changelog to the documentation PR #108 - by @gaborbernat
  • Leave the log level of the filelock logger as not set (previously was set to warning) PR #108 - by @gaborbernat

v3.3.0 (2021-10-03)

  • Drop python 2.7 and 3.5 support, add type hints PR #100 - by @gaborbernat
  • Document asyncio support - by @gaborbernat
  • fix typo PR #98 - by @jugmac00

v3.2.1 (2021-10-02)

  • Improve documentation
  • Changed logger name from filelock._api to filelock PR #97 - by @hkennyv

v3.2.0 (2021-09-30)

  • Raise when trying to acquire in R/O or missing folder PR #96 - by @gaborbernat
  • Move lock acquire/release log from INFO to DEBUG PR #95 - by @gaborbernat
  • Fix spelling and remove ignored flake8 checks - by @gaborbernat
  • Split main module PR #94 - by @gaborbernat
  • Move test suite to pytest PR #93 - by @gaborbernat

v3.1.0 (2021-09-27)

  • Update links for new home at tox-dev PR #88 - by @hugovk
  • Fixed link to License file PR #63 - by @sharkwouter
  • Adopt tox-dev organization best practices PR #87 - by @gaborbernat
  • Ownership moved from @benediktschmitt to the tox-dev organization (new primary maintainer @gaborbernat)

v3.0.12 (2019-05-18)

  • fixed setuptools and twine/warehouse error by making the license only 1 line long
  • update version for pypi upload
  • fixed python2 setup error
  • added test.py module to MANIFEST and made tests available in the setup commands issue #48
  • fixed documentation thanks to @AnkurTank issue #49
  • Update Trove classifiers for PyPI
  • test: Skip test_del on PyPy since it hangs

v3.0.10 (2018-11-01)

  • Fix README rendering on PyPI

v3.0.9 (2018-10-02)

  • PR #38 from cottsay/shebang
  • updated docs config for older sphinx compatibility
  • removed misleading shebang from module

v3.0.8 (2018-09-09)

  • updated use setuptools

v3.0.7 (2018-09-09)

  • fixed garbage collection (issue #37)
  • fix travis ci badge (use rst not markdown)
  • changed travis uri

v3.0.6 (2018-08-22)

  • clean up
  • Fixed unit test for Python 2.7
  • Added Travis banner
  • Added Travis CI support

v3.0.5 (2018-04-26)

  • Corrected the prequel reference

v3.0.4 (2018-02-01)

  • updated README

v3.0.3 (2018-01-30)

  • updated readme

v3.0.1 (2018-01-30)

  • updated README (added navigation)
  • updated documentation issue #22
  • fix the SoftFileLock test was influenced by the test for FileLock
  • undo cb1d83d issue #31

v3.0.0 (2018-01-05)

  • updated major version number due to issue #29 and issue #27
  • fixed use proper Python3 reraise method
  • Attempting to clean up lock file on Unix after release

v2.0.13 (2017-11-05)

  • changed The logger is now acquired when first needed. issue #24

v2.0.12 (2017-09-02)

  • correct spelling mistake

v2.0.11 (2017-07-19)

  • added official support for python 2 issue #20

v2.0.10 (2017-06-07)

  • updated readme

v2.0.9 (2017-06-07)

  • updated readme issue #19
  • added example PR #16
  • updated readthedocs url
  • updated change order of the examples (PR #16)

v2.0.8 (2017-01-24)

  • Added logging
  • Removed unused imports

v2.0.7 (2016-11-05)

  • fixed issue #14 (moved license and readme file to MANIFEST)

v2.0.6 (2016-05-01)

  • changed unlocking sequence to fix transient test failures
  • changed threads in tests so exceptions surface
  • added test lock file cleanup

v2.0.5 (2015-11-11)

  • Don't remove file after releasing lock
  • updated docs

v2.0.4 (2015-07-29)

  • added the new classes to __all__

v2.0.3 (2015-07-29)

  • added The SoftFileLock is now always tested

v2.0.2 (2015-07-29)

  • The filelock classes are now always available and have been moved out of the if msvrct: ... elif fcntl ... else clauses.

v2.0.1 (2015-06-13)

  • fixed issue #5
  • updated test cases
  • updated documentation
  • fixed issue #2 which has been introduced with the lock counter

v2.0.0 (2015-05-25)

  • added default timeout (fixes issue #2)

v1.0.3 (2015-04-22)

  • added new test case, fixed unhandled exception

v1.0.2 (2015-04-22)

  • fixed a timeout could still be thrown if the lock is already acquired

v1.0.1 (2015-04-22)

  • fixed issue #1

v1.0.0 (2015-04-07)

  • added lock counter, added unittest, updated to version 1
  • changed filenames
  • updated version for pypi
  • updated README, License (changed format from md to rst)
  • added MANIFEST to gitignore
  • added os independent file lock ; changed setup.py for pypi
  • Update README.md
  • initial version

Author

unknown

Info

January 20, 2023 3.8