--download: Use time.monotonic() and rework code to prevent ZeroDivisionError specific handling (#1113)

This commit is contained in:
Mickaël Schoentgen authored and GitHub committed 2021-07-29 16:05:56 +02:00
1 parent 04d05a8abd
commit de13423839
1 file changed
+9 -22
+9 -22
View File
@@ -8,7 +8,7 @@ import re
import sys import sys
import threading import threading
from mailbox import Message from mailbox import Message
from time import sleep, time from time import sleep, monotonic
from typing import IO, Optional, Tuple from typing import IO, Optional, Tuple
from urllib.parse import urlsplit from urllib.parse import urlsplit
@@ -341,7 +341,7 @@ class DownloadStatus:
assert self.time_started is None assert self.time_started is None
self.total_size = total_size self.total_size = total_size
self.downloaded = self.resumed_from = resumed_from self.downloaded = self.resumed_from = resumed_from
self.time_started = time() self.time_started = monotonic()
def chunk_downloaded(self, size): def chunk_downloaded(self, size):
assert self.time_finished is None assert self.time_finished is None
@@ -354,7 +354,7 @@ class DownloadStatus:
def finished(self): def finished(self):
assert self.time_started is not None assert self.time_started is not None
assert self.time_finished is None assert self.time_finished is None
self.time_finished = time() self.time_finished = monotonic()
class ProgressReporterThread(threading.Thread): class ProgressReporterThread(threading.Thread):
@@ -380,7 +380,7 @@ class ProgressReporterThread(threading.Thread):
self._spinner_pos = 0 self._spinner_pos = 0
self._status_line = '' self._status_line = ''
self._prev_bytes = 0 self._prev_bytes = 0
self._prev_time = time() self._prev_time = monotonic()
self._should_stop = threading.Event() self._should_stop = threading.Event()
def stop(self): def stop(self):
@@ -397,16 +397,11 @@ class ProgressReporterThread(threading.Thread):
sleep(self._tick) sleep(self._tick)
def report_speed(self): def report_speed(self):
now = monotonic()
now = time()
if now - self._prev_time >= self._update_interval: if now - self._prev_time >= self._update_interval:
downloaded = self.status.downloaded downloaded = self.status.downloaded
try:
speed = ((downloaded - self._prev_bytes) speed = ((downloaded - self._prev_bytes)
/ (now - self._prev_time)) / (now - self._prev_time))
except ZeroDivisionError:
speed = 0
if not self.status.total_size: if not self.status.total_size:
self._status_line = PROGRESS_NO_CONTENT_LENGTH.format( self._status_line = PROGRESS_NO_CONTENT_LENGTH.format(
@@ -414,10 +409,9 @@ class ProgressReporterThread(threading.Thread):
speed=humanize_bytes(speed), speed=humanize_bytes(speed),
) )
else: else:
try: percentage = (downloaded / self.status.total_size * 100
percentage = downloaded / self.status.total_size * 100 if self.status.total_size
except ZeroDivisionError: else 0)
percentage = 0
if not speed: if not speed:
eta = '-:--:--' eta = '-:--:--'
@@ -448,17 +442,10 @@ class ProgressReporterThread(threading.Thread):
actually_downloaded = ( actually_downloaded = (
self.status.downloaded - self.status.resumed_from) self.status.downloaded - self.status.resumed_from)
time_taken = self.status.time_finished - self.status.time_started time_taken = self.status.time_finished - self.status.time_started
speed = actually_downloaded / time_taken if time_taken else actually_downloaded
self.output.write(CLEAR_LINE) self.output.write(CLEAR_LINE)
try:
speed = actually_downloaded / time_taken
except ZeroDivisionError:
# Either time is 0 (not all systems provide `time.time`
# with a better precision than 1 second), and/or nothing
# has been downloaded.
speed = actually_downloaded
self.output.write(SUMMARY.format( self.output.write(SUMMARY.format(
downloaded=humanize_bytes(actually_downloaded), downloaded=humanize_bytes(actually_downloaded),
total=(self.status.total_size total=(self.status.total_size