tags:

views:

53

answers:

2

What is the best way to insure that only 1 copy of a python script is running? I am having trouble with python zombies. I tired creating a write lock using open("lock","w"), but python doesn't notify me if the file already has a write lock, it just seems to wait.

+1  A: 

Your question is similar to this one: What is the best way to open a file for exclusive access in Python?. The answers given there should help you with your issue.

(Use the flag combination portalocker.LOCK_EX!|portalocker.LOCK_NB to return quickly. If the file is locked by another process, your script should get an exception.)

David Harris
+3  A: 

Try:

import os
os.open("lock", os.O_CREAT|os.O_EXCL)

The documentation for os.open and its flags.

ChristopheD
Worked like a charm, thank you!
Rook