python文件锁

lock_file = "/tmp/%s.lock"%os.path.basename(__file__).split(".")[0]
lf = None
def check_singleton(self):
    self.lf = open(self.lock_file,'w')
    self.lf.write('%d\n'%os.getpid())
    self.lf.flush()
    try:
        fcntl.flock(self.lf, fcntl.LOCK_EX|fcntl.LOCK_NB)
    except:
        sys.exit('other instance already running')

你可能感兴趣的:(python)