Files
Mapo-IOB/IOB-PI/ReadSerialePython_2test/unix.py
T
Samuele E. Locatelli bd2720afab test read seraile iniziale
2018-10-17 09:34:33 +02:00

32 lines
854 B
Python

import fcntl
import os
#---------------------------------------------------------------
# meccanismo di file lock per evitare multiple instances
# The function will try to lock the file specified , if it success, return True, else return False.
# The nice thing is that the lock will be dropped when the program terminates.
# >>>Use :
# if not lockFile(".lock.pod"):
# sys.exit(0)
def lockFile ( lockfile ) :
fd = os.open ( lockfile , os.O_CREAT | os.O_TRUNC | os.O_WRONLY )
try:
# Request exclusive (EX) non-blocking (NB) advisory lock.
fcntl.lockf ( fd , fcntl.LOCK_EX | fcntl.LOCK_NB )
except IOError:
return False
return True
if not lockFile ( ".lockfile" ) :
print '\n noi non siamo soli ...\n'
logga ( "LOCK: Piu istanze aperte")
sys.exit ( 0 )
#- print '\n running alone ...\n'