Inter-process locking

J

Jason Friedman

I have a 3rd-party process that runs for about a minute and supports
only a single execution at a time.

$ deploy

If I want to launch a second process I have to wait until the first
finishes. Having two users wanting to run at the same time might
happen a few times a day. But, these users will not have the
skills/patience to check whether someone else is currently running.
I'd like my program to be able to detect that "deploy" is already
running, tell the user, wait a minute, try again, repeat.

I do not know whether anyone has had success with
http://pythonhosted.org/lockfile/lockfile.html.

I supose I could use http://code.google.com/p/psutil/ to check for a
process with a particular name.
 
P

Piet van Oostrum

Jason Friedman said:
I have a 3rd-party process that runs for about a minute and supports
only a single execution at a time.

$ deploy

If I want to launch a second process I have to wait until the first
finishes. Having two users wanting to run at the same time might
happen a few times a day. But, these users will not have the
skills/patience to check whether someone else is currently running.
I'd like my program to be able to detect that "deploy" is already
running, tell the user, wait a minute, try again, repeat.

I do not know whether anyone has had success with
http://pythonhosted.org/lockfile/lockfile.html.

It seems to work on Mac OS X.
I supose I could use http://code.google.com/p/psutil/ to check for a
process with a particular name.

That will quite probably give you race conditions.

File locking is generally the best solution for this kind of problems, unless you can make use of OS level semaphores.
 
P

Piet van Oostrum

Jason Friedman said:
The lockfile solution seems to be working, thank you.

There is one caveat, however. If a process that has the lock crashes without releasing the lock, the lock file will stay around and prevent other processes to acquire it. Then you will have to manually remove it. I generally prefer a solution where the pid of the locking process is written to the lock file, so that other processes trying to acquire the lock can find out if the process is still around and remove the lock file if not. I have seen such solutions on Unix systems. But I am not sure if this can be done in a platform independent way without the risk of race conditions. Maybe I have to find out.
 
J

Jason Friedman

There is one caveat, however. If a process that has the lock crashes without releasing the lock, the lock file will stay around and prevent other processes to acquire it. Then you will have to manually remove it. I generally prefer a solution where the pid of the locking process is written to the lock file, so that other processes trying to acquire the lock can find out if the process is still around and remove the lock file if not. I have seensuch solutions on Unix systems. But I am not sure if this can be done in aplatform independent way without the risk of race conditions. Maybe I haveto find out.

True, luckily my application need not be that accurate. Competition
for the lock will be rare. My present solution waits a few minutes
for the lock to become available, and if not available after that time
takes it forcefully (which lockfile handily provides). Certainly not
foolproof, but we can recover merely by re-running, so if I get an
error or two each year that is acceptable.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top