import os import fcntl import time def process_lock(): lockfile = os.path.splitext(os.path.abspath(__file__))[0] + '.lock' lockfp = open(lockfile, "w") try: fcntl.flock(lockfp, fcntl.LOCK_EX | fcntl.LOCK_NB) except IOError: return return lockfp def main(): lock = process_lock() if not lock: print("lock error") exit(1) # 処理 time.sleep(360) if __name__ == '__main__': main()