码迷,mamicode.com
首页 > 编程语言 > 详细

Python脚本防止重复执行

时间:2020-03-13 17:05:18      阅读:222      评论:0      收藏:0      [点我收藏+]

标签:create   重复执行   exce   python   try   another   process   tmp   class   

# coding: utf-8

import 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 lock
    def 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)

 

Python脚本防止重复执行

标签:create   重复执行   exce   python   try   another   process   tmp   class   

原文地址:https://www.cnblogs.com/small-wei/p/12487529.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!