Пример #1
0
class TokenBucketFiller(threading.Thread):
	"""This thread fills the token bucket every C{fill_interval_in_secs} seconds 
		with C{fill_amount} number of tokens."""

	def __init__(self, bucket, fill_interval_in_secs, fill_amount):
		"""
		@type bucket: L{TokenBucket}
		"""
		threading.Thread.__init__(self)
		self.__bucket = bucket
		self.__fill_interval = fill_interval_in_secs
		self.__fill_amount = fill_amount
		self.__should_stop = False
		self.__sleep = Sleep()
		
	def run(self):
		try:
			while not self.__should_stop:
				self.__bucket.put_tokens(self.__fill_amount)
				self.__sleep.sleep(self.__fill_interval)
		except Exception as ex:
			logging.error('In TokenBucketFiller, an exception was caught: '
				'"{}"'.format(str(ex)))
	
	## HACK: in this implementation there is a very rare situation that would
	## lead to taking up to '__fill_interval' seconds to completely stop 
	## this thread.
	def stop(self):
		self.__should_stop = True
		self.__sleep.wake_up()
		self.join()
Пример #2
0
class TokenBucketFiller(threading.Thread):
    """This thread fills the token bucket every C{fill_interval_in_secs} seconds 
		with C{fill_amount} number of tokens."""
    def __init__(self, bucket, fill_interval_in_secs, fill_amount):
        """
		@type bucket: L{TokenBucket}
		"""
        threading.Thread.__init__(self)
        self.__bucket = bucket
        self.__fill_interval = fill_interval_in_secs
        self.__fill_amount = fill_amount
        self.__should_stop = False
        self.__sleep = Sleep()

    def run(self):
        try:
            while not self.__should_stop:
                self.__bucket.put_tokens(self.__fill_amount)
                self.__sleep.sleep(self.__fill_interval)
        except Exception as ex:
            logging.error('In TokenBucketFiller, an exception was caught: '
                          '"{}"'.format(str(ex)))

    ## HACK: in this implementation there is a very rare situation that would
    ## lead to taking up to '__fill_interval' seconds to completely stop
    ## this thread.
    def stop(self):
        self.__should_stop = True
        self.__sleep.wake_up()
        self.join()
Пример #3
0
class TreeSaverThread(threading.Thread):	
	def __init__(self, dst_file_path, tree, sleep_time):
		"""
		@param sleep_time: sleep time between tree saves in seconds
		@type tree: L{RWLockTreeAccessor}
		"""
		threading.Thread.__init__(self)
		self.__path = dst_file_path
		self.__tree = tree
		self.__sleep_time = sleep_time
		self.__should_stop = False
		self.__sleep = Sleep()

	def run(self):
		while not self.__should_stop:
			self.__sleep.sleep(self.__sleep_time)
			with TempDir() as temp_dir:
				tmp_file_path = os.path.join(temp_dir.get_path(), "tree.xml")
				with open(tmp_file_path, "w") as tmp_f:
					writer = XMLTreeWriter(tmp_f)
					self.__tree.get_lock().writer_acquire()
					try:
						sentinel = self.__tree.get_sentinel()
						writer.write(sentinel)
					finally:
						self.__tree.get_lock().writer_release()
				shutil.copyfile(tmp_file_path, self.__path)
	
	def stop_activity(self):
		self.__should_stop = True
		self.__sleep.wake_up()