Singleton process
Andrew Bennetts
andrew-pythonlist at puzzling.org
Mon Dec 22 19:30:01 EST 2003
On Mon, Dec 22, 2003 at 02:13:46AM -0500, Benjamin Han wrote:
> On Mon, 22 Dec 2003, Andrew Bennetts wrote:
> > On Mon, Dec 22, 2003 at 01:38:27AM -0500, Benjamin Han wrote:
> > >
> > > 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.
This is a potential problem, but Python is much less prone to unhandled
crashes than C code. Use of a try/finally block, e.g.:
if shouldRun():
try:
main()
finally:
os.rmdir(MAGIC_PATH)
You could possibly use an atexit handler instead.
-Andrew.
More information about the Python-list
mailing list