Advertisement
rharder

Illustrate Upload Progress with aiohttp and tqdm

Mar 11th, 2017
257
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.11 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. """
  3. Illustrates how to have upload progress using tqdm and aiohttp.
  4.  
  5. PasteBin: http://pastebin.com/ksEfNJZN
  6. """
  7. import asyncio
  8. import io
  9. import os
  10.  
  11. import aiohttp
  12. import time
  13. from tqdm import tqdm
  14.  
  15. __author__ = "Robert Harder"
  16. __email__ = "rob@iharder.net"
  17. __license__ = "Public Domain"
  18.  
  19.  
  20. def main():
  21.     loop = asyncio.get_event_loop()
  22.     loop.run_until_complete(run())
  23.     loop.close()
  24.  
  25.  
  26. @asyncio.coroutine
  27. def run():
  28.     session = aiohttp.ClientSession()
  29.  
  30.     # Upload a multipart form file normally
  31.     # with open(__file__, "rb") as f:
  32.     #     resp = yield from session.post("https://transfer.sh/", data={"file": f})
  33.     # file_url = yield from resp.text()
  34.     # print(file_url)
  35.  
  36.     # Upload a multipart form file with a progress indicator
  37.     with tqio(__file__, slow_it_down=True) as f:
  38.         resp = yield from session.post("https://transfer.sh/", data={"file": f})
  39.     file_url = yield from resp.text()
  40.     print(file_url)
  41.  
  42.     yield from session.close()
  43.  
  44.  
  45. class tqio(io.BufferedReader):
  46.     def __init__(self, file_path, slow_it_down=False):
  47.         super().__init__(open(file_path, "rb"))
  48.         self.t = tqdm(desc="Upload",
  49.                       unit="bytes",
  50.                       unit_scale=True,
  51.                       total=os.path.getsize(file_path))
  52.  
  53.         # Artificially slow down transfer for illustration
  54.         self.slow_it_down = slow_it_down
  55.  
  56.     def __enter__(self):
  57.         return self
  58.  
  59.     def __exit__(self, exc_type, exc_value, traceback):
  60.         self.close()
  61.  
  62.     def read(self, *args, **kwargs):
  63.         if self.slow_it_down:
  64.             chunk = super().read(64)
  65.             self.t.update(len(chunk))
  66.             time.sleep(.1)
  67.             return chunk
  68.         else:
  69.             # Keep these three lines after getting
  70.             # rid of slow-it-down code for illustration.
  71.             chunk = super().read(*args, **kwargs)
  72.             self.t.update(len(chunk))
  73.             return chunk
  74.  
  75.     def close(self, *args, **kwargs):
  76.         self.t.close()
  77.         super().close()
  78.  
  79.  
  80.  
  81. if __name__ == '__main__':
  82.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement