How to use a timer in Python?

Nico Grubert nicogrubert at gmail.com
Fri Sep 23 05:07:27 EDT 2005


> That all sounds very race-y to me!  The cron-job and the other process
> need to take the same lock, otherwise the cron-job will start 1ms
> after the other process checks for transfer.lock and before it has a
> chance to create newfile and there will be trouble.
> 
> Using files as locks isn't brilliant because the operations "read to
> see if the lock is there" and "create the file isn't" aren't atomic.
> Ie someone can get in there after you read the directory but before
> you create the file.
> 
> However creating a directory is atomic, so you can take the lock by
> os.mkdir("/tmp/lock").  If that succeeded you got the lock, if it
> failed (threw OSError) then you didn't.  If it failed then just
> time.sleep(1) and try again.  This kind of locking works cross
> platform too.  You can use it in shell too, eg "mkdir /tmp/lock ||
> exit 1" in your cronjob.

There is no cronjob anymore now. I just need to check if there is a lock 
file. How would you modify my short program to avoid the situation "Ie 
someone can get in there after you read the directory but before
you create the file."?

Nico




More information about the Python-list mailing list