Singleton process

Benjamin Han bhan at andrew.cmu.edu
Mon Dec 22 02:13:46 EST 2003


On Mon, 22 Dec 2003, Andrew Bennetts wrote:
> On Mon, Dec 22, 2003 at 01:38:27AM -0500, Benjamin Han wrote:
> > On Mon, 22 Dec 2003, Jp Calderone wrote:
> [...]
> > >     import os, errno
> > >
> > >     def shouldRun():
> > >         try:
> > >             os.mkdir(MAGIC_PATH)
> > >         except OSError, e:
> > >             if e.args[0] == errno.EEXIST:
> > >                 return False
> > >             raise
> > >         return True
> >
> > But this solution creates a file race condition?
>
> How?  mkdir is atomic.  Either it creates the directory and succeeds, or it
> fails.  I don't see any problems here.

ok I see. The reason I'm thinking of using a lock file through fcntl is that
even if a lock file exists (maybe due to a crash of some sort before), using
the locking mechanism would still work. The "test if it exists" approach
would fail because of some leftover from some accidental 'death' of the process
before.

Any more suggestion? Thanks.





More information about the Python-list mailing list