import fcntl
pid_file ='program.pid'fp = open(pid_file,'w')
try:
fcntl.lockf(fp, fcntl.LOCK_EX | fcntl.LOCK_NB)
exceptIOError:
# another instance is running
sys.exit(0)
另外fcntl.flock是直接对整个文件进行加锁,而fcntl.lockf是可以指定对文件中的某一段加锁(参数可以指定起止位置)
import fcntl
pid_file ='program.pid'另外fcntl.flock是直接对整个文件进行加锁,而fcntl.lockf是可以指定对文件中的某一段加锁(参数可以指定起止位置)