# coding=utf8
import sys, time, threading
from d import D
class Counter():
def __init__(self, d, size):
self.d = D("{}, counter".format(d.debid))
self.counterN = None
if (size > 100 * 1000):
self.counterN = [size, 0, time.time()]
self.counterT = threading.Thread(target=self.counter, args=(self.counterN,), name="counter")
self.counterT.start()
def update(self, amount):
if self.counterN:
self.counterN[1] += amount
def stop(self):
if self.counterN:
self.counterN[1] = -1
self.counterT.join()
def counter(self, n):
self.d.log("counter started", sev=4)
while n[1] > -1:
elapsed = time.time() - n[2]
kbps = 0
if elapsed > 0: kbps = int(n[1] / (1024 * elapsed))
sys.stdout.write("\r{}%, {:4d} KB/s ".format(int(100 * n[1] / n[0]), kbps))
time.sleep(0.5)
print("", file=sys.stdout)