Reason For Question
I'm trying to create a Tkinter GUI that is able to ping a server with WebSockets. As far as I understand WebSockets needs to be used in an asynchronous environment (so using asyncio). And this does not work well with Tkinter that already has its own event loop. So I found a solution that uses Tkinter's update()
. And this has worked pretty well so far (I set the sleep time to 0.01 seconds and seems to work fine).
async def gui_refresher(_root, _gui_sleep):
"""This is where the GUI's 'frames' will be updated and asyncio's event loop is
given a chance to preform actions. """
# Run forever.
while True:
# Update the window.
root.update()
# Preform a context switch to check on other tasks for _gui_sleep's time.
await asyncio.sleep(_gui_sleep)
In the rest of my application, I have 'send' and 'receive' task that uses a WebSocket to communicate with the server. And as I've said this works well.
Question
While this works, I have read that it is not advisable to use this update()
method on the Tkinter window, since you will have nested event loops and in effect a memory leak. And here where the commenter says that you have to be careful with it, implying that it could work.
I have run my code for a few minutes looking at the memory usage doing all sorts of GUI calls and placing 200 widgets on my window. Nothing looked like a memory leak.
So my question is: when does a memory leak happen (or more precisely when does using update()
lead to a nested event loop). And if gui_refresher()
(in my code) is the only function that is using update()
will that ever lead to a memory leak?