多线程池Flask实战应用
import json
import time
import flask
from concurrent.futures import ThreadPoolExecutor
app = flask.Flask(__name__)
pool = ThreadPoolExecutor()
def read_file():
time.sleep(0.5)
return "success"
def read_db():
time.sleep(0.6)
return "success"
def read_api():
time.sleep(0.3)
return "success"
@app.route("/")
def index():
res_file = pool.submit(read_file)
res_db = pool.submit(read_db)
res_api = pool.submit(read_api)
return {
"res_file": res_file.result(),
"res_db": res_db.result(),
"res_api": res_api.result(),
}
if __name__ == '__main__':
app.run()