Skip to main content

A simple Lock Dir system. Creating Dirs is atomic, which is useful for locking things to only let one version of a process start.

A directory is created, say called ‘lock’. Inside this directory will be a PID file, which keeps the PID of the running process. If the process dies, or is killed, then the whole directory will be deleted, allowing the task to be run again later, and not leaving clutter around the place.

Usage:

Basic usage should be as simple as

try:
    with onlyonce.SingleProcessLock('/tmp/lock') as lock:
        lock.run(['rsync', '-avz', '/here', 'there'])
except onlyonce.StillRunning as err:
    print str(err)

if you want to do something special with running the task (redirecting output, etc) then you can use lock.run with the same arguments as subprocess.Popen (they’ll all get passed through). So:

lock.run(['rsync','...'], stderr=...)

Should work fine.

If you dont want to block and wait for the process to finish, then you’ll need to go a bit more low level:

try:
    with onlyonce.SingleProcessLock('/tmp/lock') as lock:
    process = Popen(['rsync', '-avz', '/here', 'there'], stderr=whatever ...)
    lock.write_pid(process.pid)
except ...

Which doesn’t block.

Notes:

You can ‘kill’ the task, and onlyonce will clean up the lock dir for you. If you ‘kill -9’ the script, however, python freaks out and quits before the script can do anything. Sorry!

This system is NOT designed to replace large daemonising clever init/systemd whatever stuff. It was written simply to let us run rsync & other processess around the campus without it being possible for multiple versions of the script to be running at once.

Ideally, you should only be running ONE process with this at a time.

Have fun. :-)

Metadata

Release files for onlyonce 0.0.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for onlyonce 0.0.3
File Size Uploaded
onlyonce-0.0.3.tar.gz 4.0 kB Details

Release files / onlyonce-0.0.3.tar.gz

Download URL onlyonce-0.0.3.tar.gz
Size 4.0 kB
Tags Source
SHA-256 checksum
How to use checksums
0ae78a5d6040a806529cdf8b463893339bf5ad38eac315d91089f8019f701e23
BLAKE2b-256 checksum
How to use checksums
0541413b83bdfce9902f76923be7211fe153080c8987b6d3ba28bb64e4b9a9f1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

This release

0.0.3 This release

1 release file

0.0.2

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page