dejsem.1.5/python/dejsem.pycharm/counter.py
changeset 0 676905a3b03c
equal deleted inserted replaced
-1:000000000000 0:676905a3b03c
       
     1 # coding=utf8
       
     2 
       
     3 import sys, time, threading
       
     4 from d import D
       
     5 
       
     6 
       
     7 class Counter():
       
     8 
       
     9 	def __init__(self, d, size):
       
    10 		self.d = D("{}, counter".format(d.debid))
       
    11 		self.counterN = None
       
    12 		if (size > 100 * 1000):
       
    13 			self.counterN = [size, 0, time.time()]
       
    14 			self.counterT = threading.Thread(target=self.counter, args=(self.counterN,), name="counter")
       
    15 			self.counterT.start()
       
    16 
       
    17 	def update(self, amount):
       
    18 		if self.counterN:
       
    19 			self.counterN[1] += amount
       
    20 
       
    21 	def stop(self):
       
    22 		if self.counterN:
       
    23 			self.counterN[1] = -1
       
    24 			self.counterT.join()
       
    25 
       
    26 	def counter(self, n):
       
    27 		self.d.log("counter started", sev=4)
       
    28 		while n[1] > -1:
       
    29 			elapsed = time.time() - n[2]
       
    30 			kbps = 0
       
    31 			if elapsed > 0: kbps = int(n[1] / (1024 * elapsed))
       
    32 			sys.stdout.write("\r{}%, {:4d} KB/s   ".format(int(100 * n[1] / n[0]), kbps))
       
    33 			time.sleep(0.5)
       
    34 		print("", file=sys.stdout)