Created
March 11, 2017 10:49
-
-
Save edsion1107/94250dc6b34279cc57361a942ab9f6eb to your computer and use it in GitHub Desktop.
python只允许启动一个实例的代码
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# -*- coding: utf-8 -*- | |
# Created by edsion on 2017/3/10 | |
import fcntl, sys, time, os | |
pidfile = 0 | |
def ApplicationInstance(): | |
global pidfile | |
pidfile = open(os.path.realpath(__file__), "r") | |
try: | |
fcntl.flock(pidfile, fcntl.LOCK_EX | fcntl.LOCK_NB) # 创建一个排他锁,并且所被锁住其他进程不会阻塞 | |
except: | |
print("another instance is running...") | |
sys.exit(1) | |
if __name__ == "__main__": | |
ApplicationInstance() | |
while True: | |
print('running...') | |
time.sleep(1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment