Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to prevent two Python programs from executing the same binary at the same time?

I have two Python scripts, that both need at regular intervals (think cronjobs) to call an external program.

If this program (over which we have no control) is called twice simultaneously, data errors occur, so we need to have a way to synchronize calls to this binary.

Is there a way to do this, preferably using only the Python standard library?

like image 616
lindelof Avatar asked Jun 18 '15 10:06

lindelof


People also ask

Can you run two Python programs at the same time?

You can create a fourth python file d.py in the same folder as other 3 python files, which imports the other 3 python files and runs their functions, as shown below. In this article, we have learnt how to run multiple python files.

Can you run two Python scripts at the same time PyCharm?

You can allow multiple runs by selecting Allow parallel run within the Edit Configurations menu.


1 Answers

So without using a 3rd-party library such as filelock you'll have to do something like this:

import os
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")

The better approach is to use filelock (if you can install 3rd party libraries):

from filelock import FileLock

with FileLock("/path/to/binary"):
    check_call("/path/to/binary")

You can easily install filelock using pip:

$ pip install filelock

See also related: Locking a file in Python

Note: There also seems to be a very similarly named package called lockfile! (Don't confuse the two!)

like image 200
James Mills Avatar answered Oct 06 '22 08:10

James Mills