Skip to main content

Small Python lib for running scripts once and once only (with file locks, etc)

Project description

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

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:

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. :-)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

onlyonce-0.0.2.tar.gz (3.8 kB view details)

Uploaded Source

File details

Details for the file onlyonce-0.0.2.tar.gz.

File metadata

  • Download URL: onlyonce-0.0.2.tar.gz
  • Upload date:
  • Size: 3.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for onlyonce-0.0.2.tar.gz
Algorithm Hash digest
SHA256 63bee74ceb9088d83611c557f0aa948c9b4526cecf7eb3a12e011b971e7e8d57
MD5 26984765c5ad9a02ebce8db5f5817916
BLAKE2b-256 3bc523f32d769ed63a4c9bad2288d09af7fce86183f4aac5ee04772b16d5b84a

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page