]> gitweb.fluxo.info Git - ckandumper.git/commitdiff
Progress bar should be local to avoid concurrency
authorSilvio Rhatto <rhatto@riseup.net>
Thu, 16 May 2019 16:25:42 +0000 (13:25 -0300)
committerSilvio Rhatto <rhatto@riseup.net>
Thu, 16 May 2019 16:25:42 +0000 (13:25 -0300)
ckandumper

index d9f179b1e45c271338f03e5a5da33e497978800d..7502afc0b48c74b7af06b3fa0f00d262960bac17 100755 (executable)
@@ -46,7 +46,7 @@ class DownloadMultiple:
         elif os.path.exists(dest) and not os.path.isdir(dest):
             raise ValueError('File exists and is not a folder:' + dest)
 
-    async def download_file(self, url, local_filename, semaphore):
+    async def download_file(self, url, local_filename, semaphore, progress_bar):
         """Downloads a file.
 
         Using wget as it is more reliable
@@ -101,12 +101,12 @@ class DownloadMultiple:
             if self.debug:
                 print(f'[{cmd!r} exited with {proc.returncode}]')
 
-            if hasattr(self.bar, 'update'):
-                self.bar.update(1)
+            if hasattr(progress_bar, 'update'):
+                progress_bar.update(1)
 
             return proc.returncode
 
-    async def gather(self, filepairs):
+    async def gather(self, filepairs, progress_bar):
         """Gather all files to be downloaded
 
         See https://stackoverflow.com/questions/50308812/is-it-possible-to-limit-the-number-of-coroutines-running-corcurrently-in-asyncio#50328882
@@ -115,17 +115,17 @@ class DownloadMultiple:
         jobs = []
 
         for url, filename in filepairs:
-          jobs.append(asyncio.ensure_future(self.download_file(url, filename, self.limit_concurrent)))
+          jobs.append(asyncio.ensure_future(self.download_file(url, filename, self.limit_concurrent, progress_bar)))
 
         await asyncio.gather(*jobs)
 
     def get(self, filepairs):
-        self.stats =  { 'exitstatus': {} }
-        self.bar   = tqdm(total=len(filepairs)) if self.progress and len(filepairs) > 1 else False
-        loop       = asyncio.get_event_loop()
+        self.stats   =  { 'exitstatus': {} }
+        progress_bar = tqdm(total=len(filepairs)) if self.progress and len(filepairs) > 1 else False
+        loop         = asyncio.get_event_loop()
 
         loop.set_debug(self.debug)
-        loop.run_until_complete(self.gather(filepairs))
+        loop.run_until_complete(self.gather(filepairs, progress_bar))
 
         if self.debug:
             print(self.stats)