Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #56718 > unrolled thread

Inter-process locking

Started byJason Friedman <jsf80238@gmail.com>
First post2013-10-11 17:44 -0600
Last post2013-10-18 20:08 -0600
Articles 5 — 2 participants

Back to article view | Back to comp.lang.python


Contents

  Inter-process locking Jason Friedman <jsf80238@gmail.com> - 2013-10-11 17:44 -0600
    Re: Inter-process locking Piet van Oostrum <piet@vanoostrum.org> - 2013-10-12 00:15 -0400
      Re: Inter-process locking Jason Friedman <jsf80238@gmail.com> - 2013-10-12 22:42 -0600
        Re: Inter-process locking Piet van Oostrum <piet@vanoostrum.org> - 2013-10-13 08:46 -0400
          Re: Inter-process locking Jason Friedman <jsf80238@gmail.com> - 2013-10-18 20:08 -0600

#56718 — Inter-process locking

FromJason Friedman <jsf80238@gmail.com>
Date2013-10-11 17:44 -0600
SubjectInter-process locking
Message-ID<mailman.1025.1381541497.18130.python-list@python.org>
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.

[toc] | [next] | [standalone]


#56723

FromPiet van Oostrum <piet@vanoostrum.org>
Date2013-10-12 00:15 -0400
Message-ID<m2vc13rup4.fsf@cochabamba.vanoostrum.org>
In reply to#56718
Jason Friedman <jsf80238@gmail.com> writes:

> 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.
-- 
Piet van Oostrum <piet@vanoostrum.org>
WWW: http://pietvanoostrum.com/
PGP key: [8DAE142BE17999C4]

[toc] | [prev] | [next] | [standalone]


#56762

FromJason Friedman <jsf80238@gmail.com>
Date2013-10-12 22:42 -0600
Message-ID<mailman.1048.1381639375.18130.python-list@python.org>
In reply to#56723
The lockfile solution seems to be working, thank you.

On Fri, Oct 11, 2013 at 10:15 PM, Piet van Oostrum <piet@vanoostrum.org> wrote:
> Jason Friedman <jsf80238@gmail.com> writes:
>
>> 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.
> --
> Piet van Oostrum <piet@vanoostrum.org>
> WWW: http://pietvanoostrum.com/
> PGP key: [8DAE142BE17999C4]
> --
> https://mail.python.org/mailman/listinfo/python-list

[toc] | [prev] | [next] | [standalone]


#56769

FromPiet van Oostrum <piet@vanoostrum.org>
Date2013-10-13 08:46 -0400
Message-ID<m261t1qqx9.fsf@cochabamba.vanoostrum.org>
In reply to#56762
Jason Friedman <jsf80238@gmail.com> writes:

> 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.
-- 
Piet van Oostrum <piet@vanoostrum.org>
WWW: http://pietvanoostrum.com/
PGP key: [8DAE142BE17999C4]

[toc] | [prev] | [next] | [standalone]


#57085

FromJason Friedman <jsf80238@gmail.com>
Date2013-10-18 20:08 -0600
Message-ID<mailman.1249.1382148520.18130.python-list@python.org>
In reply to#56769
> 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.

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.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web