Git Product home page Git Product logo

highlander's Introduction

Highlander: There can be only one...

Build Status Coverage Status

About

Highlander is a decorator to help developers ensure that their python process is only running once. This is helpful when you have a python program running on a set schedule (i.e., a cron) and you do not want one run of the program to overlap with another run. Highlander accomplishes this by creating a directory containing a file on disk that contains the current process identifier (PID) and creation time. If Highlander detects that the PID directory currently exists it reads the PID file inside it for the PID and creation time and checks to see if that process exists. If it does exist, it ends the program and logs that the program was already running. If it does not exist, Highlander removes the old process information directory and file, creates new ones, and executes the function associated with the decorator.

Installation

pip install highlander-one

Examples

An example using the default directory (i.e., current working directory):

from highlander import one

@one()
def run():
    ...

if __name__ == '__main__':
    run()

An example using a user-specified directory:

from highlander import one

@one('/tmp/my_app/.pid')
def run():
    ...

if __name__ == '__main__':
    run()

F.A.Q.

Why not use flock? Stop reinventing Unix dumb dumb!

There are three reasons I did not use flock:

  1. I knew there was no way that fcntl.flock would work the same on all operating systems and I found a lot of articles on the web stating just that. What I use in its stead is directory creation, which I believe is a much more reliable way to do locking across all operating systems and still only dependent on the file system to ensure it's atomic.

  2. If a process is killed abruptly (e.g., kill -9) with flock the file remains exclusively locked even though the process is not running. This occurs because the process did not have enough time to clean up the exclusive handle on the lock file. What this means is that when you attempt to run your program it will be unable to acquire the lock and your program will not run until you manually intervene and delete the lock file. My solution does not have this problem.

  3. If you are using the flock Unix tool in conjunction with your program its default behavior is to hang until the lock file is free and then execute the command. In my opinion, this is not ideal default behaviour because you could have a ton of processes build up over time and waste resources. What Highlander does is essentially skip that run of the program by returning immediately.

highlander's People

Contributors

chriscannon avatar dependabot[bot] avatar orthographic-pedant avatar schmich 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.