Test if Script Already Running

Jon Ribbens jon+usenet at unequivocal.eu
Thu Apr 20 06:13:12 EDT 2017


On 2017-04-20, Cameron Simpson <cs at zip.com.au> wrote:
> Others have pointed the way to an exact implementation.
>
> For myself, I like mkdir. It is portable. It is atomic. It fails if
> the target exists.  It works over NFS etc. It is easy.
>
>   os.mkdir('lock')
>   ... do stuff ...
>   os.rmdir('lock')

One downside to this is that if the process that 'holds the lock'
dies before it reaches the 'rmdir' then the lock becomes wedged.
With the 'flock' method, the operating system will automatically
undo the lock. Of course, depending on your application, it's possible
that this 'downside' may be a feature.


More information about the Python-list mailing list