Asynchronous operations
Learning how to perform actions which do not block the main thread
We had shuffle_image
in the __init__
method. Which in turn caused the application to not load until the image was fetched from the API. Let us now make another function called async_shuffle_image which does not block on the main thread.
We will be using the threading
library to achieve this. Go through this article to understand how the threading library should be used. The gist of it is that, thread is a separate flow of execution. But these threads are not actual OS threads. So things will not technically be running in parallel. This is okay since our operations are not CPU Intensive but IO intensive. If they are CPU intensive, then multiprocessing
module should be preferred.
A thread can be instantiated by using the threading.Thread(fn, args)
method.This method will return a thread which will start running once we call the .start()
method on it. You can also pickup a thread by using the .join()
function.
A daemon is a process running in the background in CS terms. The threading.Thread
function takes in a parameter called daemon
which is a boolean. If it is true, then the thread will be pushed to the background. Now let us use this knowledge to make our shuffle function async.
Let us walk through the code and note the changes which were done.
First up, we imported the
threading
moduleWe also started using spinner here. At line 20, we started the spinner and at line 30 we are stopping the spinner. This indicates some IO operation is happening.
At Line 32, we defined the
async_shuffle_image
function, which starts a daemon thread. At line 37, we changed the button to trigger the async function instead of the normal function.And finally at Line 42, we are calling the async function so that the startup time of the application is basically minimal but the image loads after a while.
Great! Now on startup, the application looks like this
Also when we click on the shuffle_button
the UI no longer freezes. Awesome!
Last updated