def _unlock(self, response): if response and response.session_file and response.session_locked: try: portalocker.unlock(response.session_file) response.session_locked = False except: # this should never happen but happens in Windows pass
def write_hosts_deny(denied_hosts): f = open(deny_file, 'w') portalocker.lock(f, portalocker.LOCK_EX) for key, val in denied_hosts.items(): if time.time() - val[1] < expiration_failed_logins: line = '%s %s %s\n' % (key, val[0], val[1]) f.write(line) portalocker.unlock(f) f.close()
def release(self): """ Writes into cron.master the time when cron job was completed """ if not self.master.closed: portalocker.lock(self.master, portalocker.LOCK_EX) logger.debug('WEB2PY CRON: Releasing cron lock') self.master.seek(0) (start, stop) = pickle.load(self.master) if start == self.now: # if this is my lock self.master.seek(0) pickle.dump((self.now, time.time()), self.master) portalocker.unlock(self.master) self.master.close()
def read_hosts_deny(): import datetime hosts = {} if os.path.exists(deny_file): hosts = {} f = open(deny_file, 'r') portalocker.lock(f, portalocker.LOCK_SH) for line in f.readlines(): if not line.strip() or line.startswith('#'): continue fields = line.strip().split() if len(fields) > 2: hosts[fields[0].strip()] = ( # ip int(fields[1].strip()), # n attemps int(fields[2].strip()) # last attempts ) portalocker.unlock(f) f.close() return hosts
def acquire(self, startup=False): """ Returns the time when the lock is acquired or None if cron already running lock is implemented by writing a pickle (start, stop) in cron.master start is time when cron job starts and stop is time when cron completed stop == 0 if job started but did not yet complete if a cron job started within less than 60 seconds, acquire returns None if a cron job started before 60 seconds and did not stop, a warning is issue "Stale cron.master detected" """ if sys.platform == 'win32': locktime = 59.5 else: locktime = 59.99 if portalocker.LOCK_EX is None: logger.warning('cron disabled because no file locking') return None self.master = fileutils.open_file(self.path, 'rb+') try: ret = None portalocker.lock(self.master, portalocker.LOCK_EX) try: (start, stop) = pickle.load(self.master) except: start = 0 stop = 1 if startup or self.now - start > locktime: ret = self.now if not stop: # this happens if previous cron job longer than 1 minute logger.warning('stale cron.master detected') logger.debug('acquiring lock') self.master.seek(0) pickle.dump((self.now, 0), self.master) self.master.flush() finally: portalocker.unlock(self.master) if not ret: # do this so no need to release self.master.close() return ret
def acquire(self, startup=False): """ Returns the time when the lock is acquired or None if cron already running lock is implemented by writing a pickle (start, stop) in cron.master start is time when cron job starts and stop is time when cron completed stop == 0 if job started but did not yet complete if a cron job started within less than 60 seconds, acquire returns None if a cron job started before 60 seconds and did not stop, a warning is issued ("Stale cron.master detected") """ if sys.platform == 'win32': locktime = 59.5 else: locktime = 59.99 if portalocker.LOCK_EX is None: self.logger.warning('cron disabled because no file locking') return None self.master = fileutils.open_file(self.path, 'rb+') ret = None try: portalocker.lock(self.master, portalocker.LOCK_EX) try: (start, stop) = pickle.load(self.master) except: start = 0 stop = 1 if startup or self.now - start > locktime: ret = self.now if not stop: # this happens if previous cron job longer than 1 minute self.logger.warning('stale cron.master detected') self.logger.debug('acquiring lock') self.master.seek(0) pickle.dump((self.now, 0), self.master) self.master.flush() finally: portalocker.unlock(self.master) if not ret: # do this so no need to release self.master.close() return ret
def worker1(fh): time.sleep(2) unlock(fh)