Skip to main content

A platform independent file lock.

Project description

This package contains a single module, which implements a platform independent file locking mechanism for Python.

The lock includes a lock counter and is thread safe. This means, that when you lock the same lock object (in the same application) twice, you will get no timeout error.

Examples

import filelock

lock = filelock.FileLock("my_lock_file")

# This is the easiest way to use the file lock. Note, that the FileLock
# object blocks until the lock can be acquired.
with lock:
        print("Doing awesome stuff")

# If you don't want to wait an undefined time for the file lock, you can use
# the *acquire* method to provide a *timeout* paramter:
try:
        with lock.acquire(timeout=10):
                print("Doing more awesome stuff!")
except filelock.Timeout as err:
        print("Could not acquire the file lock. Leaving here!")
        exit(1)

# When you're using Python 3.3+, *filelock.Timeout* is a subclass of
# *TimeoutError* else OSError. So you can do this too:
try:
        with lock.acquire(timeout=10):
                print("Doing more awesome stuff!")
except TimeoutError as err:
        print("Could not acquire the file lock. Leaving here!")
        exit(1)

# If you don't want to use or if you can't use the *with-statement*, the
# example above is equal to this one:
try:
        lock.acquire(timeout=10)
except filelock.Timeout as err:
        print("Could not acquire the file lock. Leaving here!")
        exit(1)
else:
        print("Doing more awesome stuff!")
finally:
        lock.release()

# You can even nest the lock or acquiring it multiple times in the same
# application.
with lock:
        assert lock.is_locked()
        with lock:
                assert lock.is_locked()
        assert lock.is_locked()
assert (not lock.is_locked())

License

This package is public domain.

Project details


Release history Release notifications | RSS feed

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

filelock-1.0.3.zip (7.7 kB view details)

Uploaded Source

File details

Details for the file filelock-1.0.3.zip.

File metadata

  • Download URL: filelock-1.0.3.zip
  • Upload date:
  • Size: 7.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for filelock-1.0.3.zip
Algorithm Hash digest
SHA256 7fe6c162db3d9bd17827685108696673d7ac0ce31921d64299673009128bc8f4
MD5 d8f13520718cbdeafcfe8c6ed582a6df
BLAKE2b-256 303f4db11f5062c2b4e6f25ab1b121d231c5c52dbf9d0903f688ba47dd804bda

See more details on using hashes here.

Provenance

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page