From e9cd4db258c060a884751e900dc100be5536030a Mon Sep 17 00:00:00 2001 From: bernat Date: Sun, 5 Dec 2004 13:17:13 +0100 Subject: [PATCH] Amelioration du lock (utilisation de flock pour assurer une zone exclusive). darcs-hash:20041205121713-d1718-a6be427ecc5876129ad2be67a85154ed747d7fc7.gz --- gestion/lock.py | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/gestion/lock.py b/gestion/lock.py index 3a992c45..efbac31b 100755 --- a/gestion/lock.py +++ b/gestion/lock.py @@ -10,12 +10,27 @@ Licence : GPLv2 import os,string,time,sys, affich_tools from commands import getoutput 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) : """ 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 + + # 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) : ### Lock existant @@ -65,10 +80,15 @@ def make_lock(lock_name, lock_comment='',nowait=1) : return make_lock(lock_name, lock_comment) ### 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.close() + # On enleve le verrou système + lockf(lock_fd_dl, LOCK_UN) + lock_fd_dl.close() + + def remove_lock( lock_name ) : """ Destruction du lock """ lock_file = '/var/lock/' + lock_name