Skip to content

Commit

Permalink
improvements and fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Arijit Basu committed Jan 11, 2018
1 parent 3c03948 commit 48b0560
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 22 deletions.
12 changes: 6 additions & 6 deletions README.md
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Simple PID based locking for cronjobs, UNIX scripts or python programs
* Install with pip

```bash
sudo pip install pidlock
sudo pip install -U pidlock
```

* Use it from inside python script
Expand All @@ -23,21 +23,21 @@ import time
from pidlock import PIDLock

locker = PIDLock()
with locker.lock(__file__, os.getpid()):
time.sleep(5)
with locker.lock('sleepy_script'):
time.sleep(10)
```

* Use it as commandline/cron job

```bash
pidlock 'sleep 2; sleep 2; sleep 2'
pidlock -n sleepy_script -c 'sleep 10'
```


### Customization:

You can pass PID file location and verbosity as arguments
* You can pass PID file location and verbosity as arguments

```python
locker = PIDLock(lockdir='~/anotherlockdir', verbose=False)
locker = PIDLock(lockdir='~/.pidlock', verbose=True)
```
45 changes: 29 additions & 16 deletions pidlock.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@
'''

from __future__ import print_function
import re
import os
import sys
import psutil
from contextlib import contextmanager


VERSION = 'v1.0.0'
VERSION = 'v1.0.1'

class PIDLockedException(Exception):
def __init__(self, name, pid):
Expand All @@ -27,27 +27,31 @@ class PIDLock:
RAISES:
PIDLockedException
EXAMPLE:
import time
from pidlock import PIDLock
locker = PIDLock()
with locker.lock('sleepy_script', os.getpid()):
time.sleep(5)
with locker.lock('sleepy_script'):
time.sleep(10)
"""
def __init__(self, lockdir='~/.pidlock', verbose=True):
self.lockdir = os.path.expanduser(lockdir)
self.verbose = verbose

@contextmanager
def lock(self, name, pid):
def lock(self, name):
if not os.path.isdir(self.lockdir): os.mkdir(self.lockdir)

pidfile = os.path.join(self.lockdir, name + ".pid")
pid = os.getpid()

# If lock exists
if os.path.isfile(pidfile):
f = open(pidfile)
xpid = int(f.read())
f.close()
# If pid exists, quit
pids = [int(x.split()[1]) for x in os.popen("ps -eaf").read().splitlines()[1:]]
pids = psutil.pids()
pids.remove(pid)
if xpid in pids:
raise PIDLockedException(name, xpid)
# Else remove lock
Expand All @@ -70,22 +74,31 @@ def lock(self, name, pid):
os.remove(pidfile)


def cli_lock():
def pidlock_cli():
"""
CLI interface for PID based locking
USAGE:
pidlock [-h] -n NAME -c COMMAND [--version]
EXAMPLE:
pidlock 'sleep 2; sleep 2; sleep 2'
pidlock -n sleepy_script -c 'sleep 10'
"""
commands = sys.argv[1:]
if len(commands) == 0:
return
name = re.sub('[^0-9a-zA-Z]', '', ''.join(sys.argv[1:]))
import argparse

parser = argparse.ArgumentParser(
prog='pidlock',
description='Simple PID based locking for cronjobs, UNIX scripts or python programs'
)
parser.add_argument('-n', dest='name', help='name of the lock file', required=True)
parser.add_argument('-c', dest='command', help='commands/script to be executed', required=True)
parser.add_argument('--version', action='version', version='pidlock '+VERSION)

parsed = parser.parse_args()

locker = PIDLock()
with locker.lock(name, os.getpid()):
print("Running command:"," ".join(commands))
os.system(" ".join(commands))
with locker.lock(parsed.name):
print("Running command:", parsed.command)
os.system(parsed.command)


if __name__ == "__main__":
cli_lock()
pidlock_cli()

0 comments on commit 48b0560

Please sign in to comment.