Amelioration du lock (utilisation de flock pour assurer une zone
exclusive). darcs-hash:20041205121713-d1718-a6be427ecc5876129ad2be67a85154ed747d7fc7.gz
This commit is contained in:
parent
ca632561ae
commit
e9cd4db258
1 changed files with 22 additions and 2 deletions
|
@ -10,12 +10,27 @@ Licence : GPLv2
|
||||||
import os,string,time,sys, affich_tools
|
import os,string,time,sys, affich_tools
|
||||||
from commands import getoutput
|
from commands import getoutput
|
||||||
from user_tests import getuser
|
from user_tests import getuser
|
||||||
|
from fcntl import lockf, LOCK_EX, LOCK_NB, LOCK_UN
|
||||||
|
import errno
|
||||||
|
|
||||||
def make_lock(lock_name, lock_comment='',nowait=1) :
|
def make_lock(lock_name, lock_comment='',nowait=1) :
|
||||||
""" Création d'un lock
|
""" Création d'un lock
|
||||||
si nowait=0 fait un sys.exit(254) quand un ancien lock actif est rencontré
|
si nowait=1 fait un sys.exit(254) quand un ancien lock actif est rencontré
|
||||||
"""
|
"""
|
||||||
lock_file = '/var/lock/' + lock_name
|
lock_file = '/var/lock/' + lock_name
|
||||||
|
|
||||||
|
# On créé une zone d'exclusion
|
||||||
|
lock_fd_dl=open("%s-dotlock" % lock_file, "w")
|
||||||
|
# On demande un verrou exclusif
|
||||||
|
try:
|
||||||
|
lockf(lock_fd_dl, LOCK_EX | LOCK_NB)
|
||||||
|
except IOError, e:
|
||||||
|
if e.errno not in [errno.EACCESS, errno.EAGAIN]:
|
||||||
|
raise
|
||||||
|
# La procédure de lock est deja en cours d'execution, on essaie un peu plus tard
|
||||||
|
time.sleep(0.5)
|
||||||
|
return make_lock(lock_name, lock_comment)
|
||||||
|
|
||||||
if os.path.isfile(lock_file) :
|
if os.path.isfile(lock_file) :
|
||||||
### Lock existant
|
### Lock existant
|
||||||
|
|
||||||
|
@ -65,10 +80,15 @@ def make_lock(lock_name, lock_comment='',nowait=1) :
|
||||||
return make_lock(lock_name, lock_comment)
|
return make_lock(lock_name, lock_comment)
|
||||||
|
|
||||||
### Prise du lock
|
### Prise du lock
|
||||||
lock_fd=open(lock_file, "w")
|
lock_fd = file(lock_file,"w")
|
||||||
lock_fd.write("%s\n%s\n%s" % (os.getpid(), getuser(), lock_comment) )
|
lock_fd.write("%s\n%s\n%s" % (os.getpid(), getuser(), lock_comment) )
|
||||||
lock_fd.close()
|
lock_fd.close()
|
||||||
|
|
||||||
|
# On enleve le verrou système
|
||||||
|
lockf(lock_fd_dl, LOCK_UN)
|
||||||
|
lock_fd_dl.close()
|
||||||
|
|
||||||
|
|
||||||
def remove_lock( lock_name ) :
|
def remove_lock( lock_name ) :
|
||||||
""" Destruction du lock """
|
""" Destruction du lock """
|
||||||
lock_file = '/var/lock/' + lock_name
|
lock_file = '/var/lock/' + lock_name
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue