Flask is a lightweight web framework for Python that is commonly used to build web applications. Occasionally, you may need to execute long-running tasks within a Flask application without blocking the main thread. In such cases, using threads can be a suitable approach to handle these tasks asynchronously. In this guide, we’ll explore how to use threads to execute long-running tasks in Flask.
Table of contents
1. Introduction
Flask follows a single-threaded model by default, which means that it can only handle one request at a time. However, using threads allows Flask to execute multiple tasks concurrently, including long-running tasks, without blocking the main thread and affecting the responsiveness of the application.
2. Prerequisites
Before proceeding, ensure you have the following installed:
- Python
- Flask
3. Using Threads in Flask
Below is an example of how to use threads to execute a long-running task in Flask:
from flask import Flask, jsonify
import threading
import time
app = Flask(__name__)
def long_running_task():
# Simulate a long-running task
time.sleep(5)
print("Long-running task completed.")
@app.route('/execute_task')
def execute_task():
# Start the long-running task in a separate thread
thread = threading.Thread(target=long_running_task)
thread.start()
return jsonify({'message': 'Long-running task started.'})
if __name__ == '__main__':
app.run(debug=True)
In this example:
- We define a function long_running_task() to simulate a long-running task using time.sleep().
- We define a route /execute_task that triggers the execution of the long-running task.
- When the route is accessed, the execute_task() function starts the long-running task in a separate thread using the threading.Thread() class.
- The main thread continues to handle incoming requests while the long-running task executes in the background.
4. Testing
To test the Flask application, follow these steps:
- Save the code above to a file (e.g.,
app.py
). - Run the Flask application:
python app.py
Access the /execute_task
route in your web browser or send a GET request using tools like cURL or Postman.
curl http://localhost:5000/execute_task
Verify that the message "Long-running task started."
is returned immediately, indicating that the task has been triggered in a separate thread.
After approximately 5 seconds, check the console output to confirm that the message "Long-running task completed."
is displayed, indicating that the task has finished executing.
5. Conclusion
Using threads to execute long-running tasks in Flask allows you to maintain the responsiveness of your application while handling resource-intensive operations concurrently. By leveraging threads, you can improve the performance and scalability of your Flask applications when dealing with time-consuming tasks. However, be mindful of potential thread safety issues and resource contention when using threads in a multi-threaded environment.