Skip to content

Commit

Permalink
update ratelimit class and tqdm unit_divisor
Browse files Browse the repository at this point in the history
  • Loading branch information
yodeng committed Sep 14, 2022
1 parent ed86765 commit dab3663
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 14 deletions.
10 changes: 5 additions & 5 deletions src/src.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@ def __init__(self, url="", outfile="", threads=Chunk.MAX_AS, headers={}, quite=F
self.quite = quite
self.extra = remove_empty_items(kwargs)
self.max_speed = hs_decode(self.extra.get("max_speed") or sys.maxsize)
self.chunk_size = 102400
self.chunk_size = 100 * Chunk.OneK
self.ftp = False
self.startime = int(time.time())
self.rate_limiter = RateLimit(
max(int(float(self.max_speed)/self.chunk_size), 1))

async def get_range(self, session=None, size=1024 * 1000):
async def get_range(self, session=None, size=1000*Chunk.OneK):
if os.path.isfile(self.rang_file) and os.path.isfile(self.outfile):
self.load_offset()
content_length = 0
Expand Down Expand Up @@ -105,7 +105,7 @@ async def download(self):
self.url, self.outfile)
self.loger.debug("Ranges: %s, Sem: %s, Connections: %s, %s", self.parts,
self.threads, self.tcp_conn or 100, get_as_part(self.content_length))
with tqdm(disable=self.quite, total=int(self.content_length), initial=self.tqdm_init, unit='', ascii=True, unit_scale=True) as bar:
with tqdm(disable=self.quite, total=int(self.content_length), initial=self.tqdm_init, unit='', ascii=True, unit_scale=True, unit_divisor=1024) as bar:
tasks = []
self.rate_limiter.refresh()
for h_range in self.range_list:
Expand Down Expand Up @@ -139,7 +139,7 @@ async def download_ftp(self):
human_size(self.content_length), self.content_length)
self.loger.info("Starting download %s --> %s",
self.url, self.outfile)
with tqdm(disable=self.quite, total=int(self.content_length), initial=size, unit='', ascii=True, unit_scale=True) as bar:
with tqdm(disable=self.quite, total=int(self.content_length), initial=size, unit='', ascii=True, unit_scale=True, unit_divisor=1024) as bar:
self.loger.debug(
"Start %s %s", asyncio.current_task().get_name(), 'bytes={0}-{1}'.format(size, self.content_length))
async with client.download_stream(filepath, offset=size) as stream:
Expand Down Expand Up @@ -216,7 +216,7 @@ async def download_s3(self):
self.url, self.outfile)
self.loger.debug("Ranges: %s, Sem: %s, %s", self.parts,
self.threads, get_as_part(self.content_length))
with tqdm(disable=self.quite, total=int(self.content_length), initial=self.tqdm_init, unit='', ascii=True, unit_scale=True) as bar:
with tqdm(disable=self.quite, total=int(self.content_length), initial=self.tqdm_init, unit='', ascii=True, unit_scale=True, unit_divisor=1024) as bar:
self.rate_limiter.refresh()
with ThreadPoolExecutor(min(self.threads, MAX_S3_CONNECT)) as exector:
tasks = []
Expand Down
18 changes: 9 additions & 9 deletions src/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,18 +118,12 @@ class TimeoutException(Exception):
pass


def now():
if hasattr(time, 'monotonic'):
return time.monotonic
return time.time


class RateLimit(object):
def __init__(self, calls=15, period=1, clock=now()):
def __init__(self, calls=15, period=1):
self.clamped_calls = max(1, min(sys.maxsize, floor(calls)))
self.period = period
self.clock = clock
self.last_reset = clock()
self.clock = self._now
self.last_reset = self._now()
self.num_calls = 0
self.lock = RLock()

Expand All @@ -150,6 +144,12 @@ def _period_remaining(self):
def refresh(self):
self.last_reset = self.clock()

@property
def _now(self):
if hasattr(time, 'monotonic'):
return time.monotonic
return time.time


def human_size(num):
for unit in ['B', 'K', 'M', 'G', 'T', 'P', 'E', 'Z']:
Expand Down

0 comments on commit dab3663

Please sign in to comment.