asynchronous execute python app with ThreadPoolExecutor

we can launch parallel tasks with python concurrent.futures to double the speed.
here is the original code with one thread one_thread.py:

import flask
import json
import time

app = flask.Flask(__name__)

def read_file():
	time.sleep(0.1)
	return "file result"
	
def read_db():
	time.sleep(0.2)
	return "db result"
	
def read_api():
	time.sleep(0.3)
	return "api result"
	
@app.route("/")
def index():
	result_file = read_file()
	result_db = read_db()
	result_api = read_api()
	
	return json.dumps({
		"result_file": result_file,
		"result_db": result_db,
		"result_api": result_api,
	})
	
if __name__ == "__main__":
	app.run(host='0.0.0.0')

and another one multiple_threads.py

import flask
import json
import time
from concurrent.futures import ThreadPoolExecutor

app = flask.Flask(__name__)
pool = ThreadPoolExecutor()

def read_file():
	time.sleep(0.1)
	return "file result"
	
def read_db():
	time.sleep(0.2)
	return "db result"
	
def read_api():
	time.sleep(0.3)
	return "api result"
	
@app.route("/")
def index():
	result_file = pool.submit(read_file)
	result_db = pool.submit(read_db)
	result_api = pool.submit(read_api)
	
	return json.dumps({
		"result_file": result_file.result(),
		"result_db": result_db.result(),
		"result_api": result_api.result(),
	})
	
if __name__ == "__main__":
	app.run(host='0.0.0.0')

run curl command to compare the real time spend:

output from one_thread.py:
ubuntu@ubuntu2020:~$ time curl http://192.168.0.171:5000
{"result_file": "file result", "result_db": "db result", "result_api": "api result"}
real    0m0.629s
user    0m0.009s
sys     0m0.013s

output from multiple_threads.py:
ubuntu@ubuntu2020:~$ time curl http://192.168.0.171:5000
{"result_file": "file result", "result_db": "db result", "result_api": "api result"}
real    0m0.328s
user    0m0.014s
sys     0m0.010s

the real time is dropped from 629ms to 328ms with multiple threads!


Leave a Reply

Your email address will not be published. Required fields are marked *