Giter Site home page Giter Site logo

pylockfile's Introduction

pylockfile

Python implementation of Lock (.lock) and PID (.pid) file concepts

A lock file is a file used by various operating systems and programs to lock a resource, such as a file or device. A lock file is just a file, but if it exists, it signals to other processes that the resource is already in use. The pylockfile.lockfile.LockFile class can create lock files, delete them, and raise exceptions when attempts to lock it again. This class can be used as a Context Manager or Decorator for critical code sections or functions.

A PID file is a similar concept. It is a file that is created when a process starts and deleted when the process finishes. Usually, a PID file contains the process identifier number in its name. The pylockfile.pidfile.PidFile class can create a PID file when a process starts and delete it when it ends. It can also correctly handle SIGTERM and SIGINT signals and can work as a Context Manager.

Last but not least, the signal_dispatcher module contains the pylockfile.signal_dispatcher.SignalDispatcher class. This module allows for re-assignment or addition of new handlers for system signals such as SIGTERM or SIGINT. This module is used by other modules in the package, but it can also be used separately if you need to set your own handlers for system signals.

Features:

  • LockFile and PidFile can be used as Context Managers
  • LockFile and PidFile can be used as Decorators
  • Available class SignalPidFile, it is singleton version of PidFile
  • Available .lock \ .pid file removing by handling of SIGTERM \ SIGINT signals

Example:

    import time
    import os
    from pylockfile.lockfile import LockFile
    from pylockfile.pidfile import SinglePidFile
    from pylockfile.lock_exceptions import AlreadyLocked

    with SinglePidFile(delete_lock_on_sigint=True):
        spf = SinglePidFile()
        while spf.is_locked():
            try:
                with LockFile(lockname="critical_code") as lockname:
                    print(f"I, process with PID={os.getpid()}, have caught this very critical resource")
                    # work with some critical resource
                    time.sleep(5)
            except AlreadyLocked:
                print(f"But I, process with PID={os.getpid()},can not caught this very critical resource")

            @LockFile(lockname="critical_function")
            def some_critical_function():
                time.sleep(3)
                print(f"Process with PID={os.getpid()},in some critical function")
            try:
                some_critical_function()
            except AlreadyLocked:
                print(f"Process with PID={os.getpid()},awaiting access to some critical function")

pylockfile's People

Contributors

l0andr 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.