Giter VIP home page Giter VIP logo

justpid's Introduction

Introduction

The JustPidFile libraray is a very simple, yet functional, pid file implementation.

This way the first process accessing the pid file, is able to write there its PID (Process ID). The next one will check the file, the pid inside, and will check if the process with the PID exists. If it exists, then an exception is thrown.

If the pid file doesn't exist, or contains garbage data, or an integer with a PID of a not running process, then the directory can be "locked".

"Locking" a directory is done by creating a ".pid" file in the directory with the locking process ID inside.

Gotchas

Locking a directory with a simple file works as long as everybody uses the same mechanism. There is nothing that forbids any other process to change files in the "locked" directory. The purpose of this library is to avoid having two processes writing to the same files at the same time.

Example

Lock a directory using the simple functional API

import justpid as jp
directory = "abc"

try:
    jp.lock(directory)
    # some logic
    jp.unlock(directory)
except jp.LockException:
    print("Cannot lock a directory")

Use a context manager to automatically unlock the directory at the end

import justpid as jp

with jp.Lock(directory):
    # do something here
    # the directory is exlusively locked
# and here is unlocked

Implementation Details

There are some hardcoded things like:

  • The pid file is in side the locked directory and is named .pid
  • The main api is made of functions.
  • There is also a context manager, just to make life a little bit simpler.

Do You Need this?

Maybe? Who knows. I need.

justpid's People

Contributors

szymonlipinski avatar

Watchers

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