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
  • 66,304
  • 38
  • 162
  • 239

2 Answers2

3

Try:

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

The documentation for os.open and its flags.

ChristopheD
  • 112,638
  • 29
  • 165
  • 179
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,332
  • 1
  • 13
  • 25