Giter Site home page Giter Site logo

dir_lock's Introduction

DirLock

A very simple directory lock using a temporary lock file. While the lock is present, another process cannot write into the directory. At least if it is using the same lock mechanism, of course any process can write into the directory if it likes...

Apparently this seems to be much cooler: https://pypi.org/project/filelock/

FAQ

  • Is this tested?
    • yes
  • Does it always work as advertised?
    • Probably not
  • Install with pip install [--user] . when in the directory with the setup.py in it

Usage

The DirLock acts as a context manager and cleans up after itself:

from dir_lock import DirLock

# The directory we want to lock is the parent of the given lock filename
lock_file = "./work_dir/_lock"
print("I want to lock '{}'".format(os.path.dirname(lock_file)))

with DirLock(lock_file, create_dir=True) as dlock:
    print("Created lockfile '{}', sleeping for 5s".format(dlock.lock_file))
    time.sleep(5.)

assert not os.path.isfile(lock_file)
print("Done, left lock. Lockfile is gone")

Example

Also try the example script in two different processes:

Example

dir_lock's People

Contributors

maxnoe avatar mennthor avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.