From: NeilBrown Date: Sun, 22 Apr 2012 04:22:53 +0000 (+1000) Subject: suspend.py: make a 'blocker' class. X-Git-Url: http://git.neil.brown.name/?a=commitdiff_plain;h=df22a8feb519821852b5996350d1917d8e33633e;p=susman.git suspend.py: make a 'blocker' class. Rather than using a global variable to hold the file handle on the 'disabled' file, create a class so a class-object can hold it. --- diff --git a/suspend.py b/suspend.py index f070334..842b9a5 100644 --- a/suspend.py +++ b/suspend.py @@ -101,22 +101,21 @@ class monitor: self.immediate_fd.close() self.immediate_fd = None -blockfd = None -def block(): - global blockfd - if blockfd: - return - try: - blockfd = open('/var/run/suspend/disabled') - fcntl.flock(blockfd, fcntl.LOCK_SH) - except: - pass +class blocker: + def __init__(self, blocked = True): + self.blockfd = open('/var/run/suspend/disabled') + if blocked: + self.block() + def block(self): + fcntl.flock(self.blockfd, fcntl.LOCK_SH) + def unblock(self): + fcntl.flock(self.blockfd, fcntl.LOCK_UN) + def close(self): + self.blockfd.close() + self.blockfd = None + def abort(self): + self.blockfd.read(1) -def unblock(): - global blockfd - if blockfd: - blockfd.close() - blockfd = None def abort_cycle(): fd = open('/var/run/suspend/disabled')