Take the 2-minute tour ×

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.

share|improve this question

marked as duplicate by ᾠῗᵲᄐᶌ, the Tin Man, KingCrunch, j0k, Celada Aug 31 '12 at 10:28


add comment

2 Answers

up vote 3 down vote accepted


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

The documentation for os.open and its flags.

share|improve this answer
Worked like a charm, thank you! –  Rook Jul 13 '10 at 22:01
This will create a file named lock. This file will not be deleted automatically. –  A-B-B Feb 20 '13 at 22:08
add comment

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

share|improve this answer
add comment
