Understanding asynchronous operations
Some of the operations in a web application can be time-consuming and make the overall application feel slow for the user, even though it’s not actually slow. This hampers the user experience significantly. To deal with this, the simplest way to implement the asynchronous execution of operations is with the help of threads. In this recipe, we will implement this using the threading
libraries of Python. In Python 3, the thread
package has been deprecated. Although it is still available as _thread
, it is highly recommended to use threading
.
Getting ready
We will use the application from the Implementing email support for Flask applications recipe. Many of us will have noticed that, while the email is being sent, the application waits for the whole process to finish, which is unnecessary. Email sending can be easily done in the background, and our application can become available to the user instantaneously.