Singleton process

Benjamin Han bhan at andrew.cmu.edu
Mon Dec 22 01:38:27 EST 2003


But this solution creates a file race condition?

Also I just realized that lock (via fcntl.lockf() ) is actually not useful
since in this case scripts will come and go - locks will be lost whenever
a process terminates.

There should a code pattern for this - any more ideas? Thanks!

On Mon, 22 Dec 2003, Jp Calderone wrote:
> On Sun, Dec 21, 2003 at 09:38:49PM -0800, Fortepianissimo wrote:
> > Here is the situation: I have multiple processes of same Python script
> > fired, but I want *only one* of them to continue and all the others to
> > quit immediately.
> >
> > I can use a lock file, and the first process will get the necessary
> > lock. But if I do open(lockfile) all the other subsequent processes
> > will just wait there - instead I want them to quit immediately.
> >
> > Can someone give a simple outline of how to achieve this? Thanks a
> > lot.
>
>     import os, errno
>
>     def shouldRun():
>         try:
>             os.mkdir(MAGIC_PATH)
>         except OSError, e:
>             if e.args[0] == errno.EEXIST:
>                 return False
>             raise
>         return True
>
>   Jp
>





More information about the Python-list mailing list