# coding: utf-8import os
import sys
import time
import fcntl
class Lock:
def__init__(self, filename):
self.filename = filename
# This will create it if does not exist already
self.handle = open(filename, ‘w‘)
# Bitwise Or fcntl.LOCK_NB if you need a non-blocking lockdef acquire(self):
fcntl.flock(self.handle, fcntl.LOCK_EX | fcntl.LOCK_EX_NB)
def__del__(self):
self.handle.close()
lock = Lock(os.path.join(‘/‘,‘tmp‘,os.path.basename(sys.argv[0]) + ‘_tmp‘))
try:
lock.acquire()
except:
print"%s [ERROR] There is already another process running!"
sys.exit(1)