本文主要介绍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
The Posts
- Python 并发编程实战 使用队列(Queue)实现生产者、消费者多线程爬虫Jul 7, 2022
- Python threading单线程与多线程爬虫实战速度测试对比Jul 4, 2022
- Python flask web项目使用线程池ThreadPoolExecutor并发执行实战Jul 4, 2022
- Laravel 9 基于自定义令牌的 Api 身份验证教程May 14, 2022
- 使用Laravel9的Sanctum REST API 身份验证示例May 14, 2022
- Laravel9的HttpBasicAuth基本认证May 9, 2022
- Markdown与html互转markdown.js_showdown.js_marked.js_turndown.jsApr 5, 2022
- WordPress后台在线更新失败问题解决Apr 4, 2022
- WordPress固定链接伪静态.html结尾Mar 28, 2022
- WordPress主题插件安装下载Mar 26, 2022
- 个人网站搭建流程及免费源码推荐Mar 26, 2022
- ES11新特性_matchAll_私有属性_allSettled_可选链操作符_动态import导入_BigInt_globalThis对象Mar 24, 2022
- ES10新特性_fromEntries_trimStart_trimEnd_flat_flatMap_descriptionMar 24, 2022
- ES9_正则扩展_正则表达式_命名捕获分组_反向断言_dotAll 模式Mar 23, 2022
- ES9_rest参数与es9_spread扩展运算符Mar 23, 2022
- ES8新特性_ECMAScript8_object对象方法扩展Mar 19, 2022
- ES8新特性_ECMAScript8_async_awaitMar 19, 2022
- Sublime Text3 配置Python3 详细教程Mar 19, 2022
- ES7新特性_ECMAScript7_includes_指数运算符Mar 19, 2022
- ES6新特性_模块化(module)Mar 17, 2022