Python flask web项目使用线程池ThreadPoolExecutor并发执行实战

Python flask web项目使用线程池ThreadPoolExecutor并发执行实战

本文主要介绍Python flask web项目使用线程池ThreadPoolExecutor并发执行,加速网页的加载速度!

首先未使用ThreadPoolExecutor线程池并发执行

import flask, json, 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()

启动flask

python test.py

(venv) fujie@fujuhaos-MacBook-Pro thread_pool_executor % python test.py
 * Serving Flask app 'test' (lazy loading)
 * Environment: production
   WARNING: This is a development server. Do not use it in a production deployment.
   Use a production WSGI server instead.
 * Debug mode: off
 * Running on http://127.0.0.1:5000 (Press CTRL+C to quit)

现在在mac终端上来查看一下运行需要的时间

time curl http://127.0.0.1:5000/

# 打印结果 总共用时: 0.621
{"result_file": "file result", "result_db": "db result", "result_api": "api result"}curl http://127.0.0.1:5000/  0.00s user 0.01s system 1% cpu 0.621 total

使用ThreadPoolExecutor线程池并发执行

import flask, json, time

# 导入ThreadPoolExecutor
from concurrent.futures import ThreadPoolExecutor

app = flask.Flask(__name__)

# 初始化一个全局ThreadPoolExecutor
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()

启动flask

python test2.py

* Serving Flask app 'test2' (lazy loading)
 * Environment: production
   WARNING: This is a development server. Do not use it in a production deployment.
   Use a production WSGI server instead.
 * Debug mode: off
 * Running on http://127.0.0.1:5000 (Press CTRL+C to quit)

在mac终端上来查看一下运行需要的时间

time curl http://127.0.0.1:5000/

# 打印结果 总共用时: 0.330, 差不多用时减掉一半,效果非常明显
{"result_file": "file result", "result_db": "db result", "result_api": "api result"}curl http://127.0.0.1:5000/  0.00s user 0.01s system 3% cpu 0.330 total
Loading...