1 蓝图的使用
| |
| |
| |
| |
| no_blueprint_flask |
| src |
| __init__.py |
| models.py |
| views.py |
| static |
| templates |
| home.html |
| manage.py |
| |
| |
| |
| |
| -第一步:导入蓝图类 from flask import Blueprint |
| -第二步:实例化得到蓝图对象 us=Blueprint('user',__name__) |
| -第三步:在app中注册蓝图 app.register_blueprint(us) |
| -第四步:在不同的views.py 使用蓝图注册路由 @us.route('/login') |
| -补充:蓝图可以有自己的静态文件和模板 |
| -补充:注册蓝图时,可以使用前缀,必须以/ 开头 |
| |
| little_blueprint |
| -src |
| -static |
| -1.jpg |
| -templates |
| -user.html |
| -views |
| -order.py |
| -user.py |
| -__init__.py |
| -models.py |
| -manage.py |
| |
| |
| |
| big_blueprint |
| -src |
| -admin |
| -static |
| -1.jpg |
| -templates |
| -admin_home.html |
| -__init__.py |
| -models.py |
| -views.py |
| -home |
| -order |
| -__init__.py |
| -settings.py |
| -manage.py |
| |
2 g对象
| |
| -global的缩写,再python中是个关键字,不能以关键字作为变量名,干脆用了g |
| -g 对象,在整个请求的全局,可以放值,可以取值 |
| -全局变量,在任意位置导入使用即可 |
| |
| -它为什么不学django使用request作为上下文? |
| -因为使用request,可能会造成request数据的污染,不小心改了request的属性,但你不知道 |
| -建议使用g 是空的,放入之后在当次请求中全局优先 |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| -g 是只针对于当次请求 |
| -session针对于多次请求 |
| |
| from flask import Flask, g, request |
| |
| app = Flask(__name__) |
| app.debug = True |
| |
| |
| @app.before_request |
| def before(): |
| if 'home' in request.path: |
| g.xx = 'xx' |
| |
| |
| def add(a, b): |
| |
| print('---', request.name) |
| return a + b |
| |
| |
| @app.route('/') |
| def index(): |
| print(g.xx) |
| name = request.args.get('name') |
| |
| request.method = name |
| res = add(1, 2) |
| print(res) |
| return 'index' |
| |
| |
| @app.route('/home') |
| def home(): |
| print(g.xx) |
| return 'index' |
| |
| |
| if __name__ == '__main__': |
| app.run() |
| |
3 数据库连接池
| |
| -使用pymysql |
| -在视图函数中,创建pymysql的连接,查数据,查完,返回给前端 |
| -有什么问题? 来一个请求,创建一个连接,请求结束,连接关闭 (djanog就是这么做的) |
| |
| -把连接对象,做成全局的,在视图函数中,使用全局的连接,查询,返回给前端 |
| -有什么问题?会出现数据错乱,详见下图 |
| |
| |
| -数据库连接池 |
| -创建一个全局的池 |
| -每次进入视图函数,从池中取一个连接使用,使用完放回到池中,只要控制池的大小,就能控制mysql连接数 |
| |
| |
| |
| |
| -1 安装 pip install dbutils |
| -2 使用:实例化得到一个池对象 |
| |
| -3 在视图函数中导入使用 |
| conn = pool.connection() |
| cursor = conn.cursor(pymysql.cursors.DictCursor) |
| cursor.execute('select id,title,author_img from aritcle limit 2') |
| res = cursor.fetchall() |
| |
| |
| |
| |
| |
| |
| @app.route('/article_pool') |
| def article_pool(): |
| conn = pool.connection() |
| cursor = conn.cursor(pymysql.cursors.DictCursor) |
| cursor.execute('select id,title,author_img from aritcle limit 2') |
| res = cursor.fetchall() |
| print(res) |
| return jsonify(res) |
| |
| |
| @app.route('/article') |
| def article(): |
| conn = pymysql.connect(user='root', |
| password="", |
| host='127.0.0.1', |
| database='cnblogs', |
| port=3306) |
| cursor = conn.cursor(pymysql.cursors.DictCursor) |
| time.sleep(random.randint(1,3)) |
| cursor.execute('select id,title,author_img from aritcle limit 2') |
| res = cursor.fetchall() |
| cursor.close() |
| conn.close() |
| return jsonify(res) |
| |
| |
| |
| |
| from threading import Thread |
| import requests |
| |
| |
| def task(): |
| res = requests.get('http://127.0.0.1:5000/article_pool') |
| print(len(res.text)) |
| |
| |
| if __name__ == '__main__': |
| for i in range(500): |
| t = Thread(target=task) |
| t.start() |
| |
| 使用池的连接数明显小 |
| 不使用池连接数明显很大 |
| |
| |
| |
| show status like 'Threads%' |

【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 25岁的心里话
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现