Flask API — How to return response but continue execution

Zoltan Varadi
2 min readJul 29, 2020

--

TLDR; After receiving the API request, create a new thread and run your function there. You can pass your desired data in kwargs.

We’ve all been there. What to do when you want to use python in some batch process for your calculations but do not want to wait for the response 10 minutes after triggering your API endpoint. Here’s what you can do.

Our Scenario

  • Google Cloud Run: Python Flask API, ready to do some tedious long running task
  • Google Cloud Scheduler: send a POST request every hour to our Flask API
  • Execution takes around 10 minutes.
  • We do not want to keep Cloud Scheduler hanging for 10 minutes, instead we send back a response immediately and we want the API to continue execution.

Solution

  • We separate our endpoint and the long running task
  • We create a new thread and execute the long running task in that thread.
import threadingthread = threading.Thread(target=long_running_task, kwargs={
'post_data': data})
thread.start()
  • We also pass the POST data we received using kwargs.
  • We return a 202 Accepted HTTP response immediately from the endpoint while it continues processing.

Code

Flask API sample code

main.py

from flask import Flask, request
import time
import threading
app = Flask(__name__)@app.route('/start_task', methods=['POST'])
def start_task():
data = request.get_json()
def long_running_task(**kwargs):
your_params = kwargs.get('post_data', {})
print("Starting long task")
print("Your params:", your_params)
for _ in range(10):
time.sleep(1)
print(".")
thread = threading.Thread(target=long_running_task, kwargs={
'post_data': data})
thread.start()
return {"message": "Accepted"}, 202if __name__ == "__main__":
app.run(host="0.0.0.0", port=5000, debug=True)

Test on local

Start your Flask app by running:

python main.py

Next, send a POST request to your localhost

curl -X POST -H “Content-Type: application/json” http://localhost:5000/start_task -d ‘{“a”:”b”}’

You will get back a response immediately and the code will continue execution.

--

--

Zoltan Varadi
Zoltan Varadi

Written by Zoltan Varadi

Data Scientist / full stack system engineer

Responses (6)