3

Possible Duplicate:
Python: single instance of program

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.

Community
  • 1
  • 1
rook
  • 64,668
  • 37
  • 156
  • 236

2 Answers2

3

Try:

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

The documentation for os.open and its flags.

ChristopheD
  • 106,997
  • 27
  • 158
  • 177
1

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.)

Community
  • 1
  • 1
David Harris
  • 2,304
  • 1
  • 13
  • 25