from subprocess import check_call
if os.path.exists('/var/run/foo.ock'):
    print('Backing off...')
raise SystemExit(1)
try:
    with open('/var/run/foo.lock', 'w'):
        check_call('/path/to/binary')
finally:
    os.remove('/var/run/foo.lock')
Is there a way to prevent two Python programs from executing the same binary at the same time?