Archive

Posts Tagged ‘python’

Show Upload Progress with Python aiohttp and tqdm

March 11th, 2017 No comments

I wanted to be able to show file upload progress for my Python asyncio-based Pushbullet.com library but could not find any obvious solution using aiohttp and tqdm. Here is what I created. Essentially I intercept the calls to reading a file and update the tqdm progress bar accordingly.

The key elements look like this:

    class tqio(io.BufferedReader):
        def __init__(self, file_path):
            super().__init__(open(file_path, "rb"))
            self.t = tqdm(desc="Upload",
                          unit="bytes",
                          unit_scale=True,
                          total=os.path.getsize(file_path))

        def read(self, *args, **kwargs):
            chunk = super().read(*args, **kwargs)
            self.t.update(len(chunk))
            return chunk

Read more…

Categories: Utility Tags: , ,

Python asyncio and tkinter Together with TCP and UDP

February 3rd, 2017 No comments

I finally figured out at least one way to have a Python tkinter app use asyncio event loops, so I thought I should share it. In the end, the solution was simply running an asyncio event loop on another thread. I hope one day Python will merge the two event loops, but for now this works OK.

I have this on pastebin also:

The key points are extracted here:

        # Thread that will handle io loop
        def _run(loop):
            asyncio.set_event_loop(loop)
            loop.run_forever()

        ioloop = asyncio.new_event_loop()
        asyncio.run_coroutine_threadsafe(_connect(), loop=ioloop)  # Schedules connection
        t = threading.Thread(target=partial(_run, ioloop))
        t.daemon = True  # won't hang app when it closes
        t.start()  # Server will connect now

Read more…

Categories: Utility Tags: , , ,