flask - 数据库连接池
flask
详细
-
编程语言及他们之间的区别?
C/C++ ,很多语言的底层实现都是与C,代码执行效率高,自己做内存管理,对代码要求比较高,很多功能需要手动试下。 Java,比较好的编程语言,很多企业级应用都会选择java。 C#,是微软开始的编程语言,部署时需要放在windown server 上,最大弊端是window系统花钱。 PHP,一般用于快速搭建网站。 Python,简洁且入门简单,很少的代码就可以做很多事情。 Golang,语法和C比较接近,处理并发时比较有优势 + docker。
1.数据库连接池
1.1 初级阶段
import pymysql
from flask import Flask
app = Flask(__name__)
def fetchall(sql):
conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='123', db='t1')
cursor = conn.cursor()
cursor.execute(sql)
result = cursor.fetchall()
cursor.close()
conn.close()
return result
@app.route('/login')
def login():
result = fetchall('select * from user')
return 'login'
@app.route('/index')
def index():
result = fetchall('select * from user')
return 'xxx'
@app.route('/order')
def order():
result = fetchall('select * from user')
return 'xxx'
if __name__ == '__main__':
app.run()
1.2 老板给修改
import pymysql
from flask import Flask
app = Flask(__name__)
CONN = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='123', db='t1')
def fetchall(sql):
cursor = CONN.cursor()
cursor.execute(sql)
result = cursor.fetchall()
cursor.close()
return result
@app.route('/login')
def login():
result = fetchall('select * from user')
return 'login'
@app.route('/index')
def index():
result = fetchall('select * from user')
return 'xxx'
@app.route('/order')
def order():
result = fetchall('select * from user')
return 'xxx'
if __name__ == '__main__':
app.run()
1.3 数据库连接池
安装
pip3 install dbutils
pip3 install pymysql
使用
import pymysql
from DBUtils.PooledDB import PooledDB
POOL = PooledDB(
creator=pymysql, # 使用链接数据库的模块
maxconnections=6, # 连接池允许的最大连接数,0和None表示不限制连接数
mincached=2, # 初始化时,链接池中至少创建的链接,0表示不创建
blocking=True, # 连接池中如果没有可用连接后,是否阻塞等待。True,等待;False,不等待然后报错
ping=0, # ping MySQL服务端,检查是否服务可用。# 如:0 = None = never, 1 = default = whenever it is requested, 2 = when a cursor is created, 4 = when a query is executed, 7 = always
host='127.0.0.1',
port=3306,
user='root',
password='222',
database='cmdb',
charset='utf8'
)
# 去连接池中获取一个连接
conn = POOL.connection()
cursor = conn.cursor()
cursor.execute('select * from web_models_disk')
result = cursor.fetchall()
cursor.close()
# 将连接放会到连接池
conn.close()
print(result)
多线程测试
import pymysql
from DBUtils.PooledDB import PooledDB
POOL = PooledDB(
creator=pymysql, # 使用链接数据库的模块
maxconnections=6, # 连接池允许的最大连接数,0和None表示不限制连接数
mincached=2, # 初始化时,链接池中至少创建的链接,0表示不创建
blocking=True, # 连接池中如果没有可用连接后,是否阻塞等待。True,等待;False,不等待然后报错
ping=0, # ping MySQL服务端,检查是否服务可用。# 如:0 = None = never, 1 = default = whenever it is requested, 2 = when a cursor is created, 4 = when a query is executed, 7 = always
host='127.0.0.1',
port=3306,
user='root',
password='222',
database='cmdb',
charset='utf8'
)
def task(num):
# 去连接池中获取一个连接
conn = POOL.connection()
cursor = conn.cursor()
# cursor.execute('select * from web_models_disk')
cursor.execute('select sleep(3)')
result = cursor.fetchall()
cursor.close()
# 将连接放会到连接池
conn.close()
print(num,'------------>',result)
from threading import Thread
for i in range(57):
t = Thread(target=task,args=(i,))
t.start()
基于函数实现sqlhelper
import pymysql
from DBUtils.PooledDB import PooledDB
POOL = PooledDB(
creator=pymysql, # 使用链接数据库的模块
maxconnections=6, # 连接池允许的最大连接数,0和None表示不限制连接数
mincached=2, # 初始化时,链接池中至少创建的链接,0表示不创建
blocking=True, # 连接池中如果没有可用连接后,是否阻塞等待。True,等待;False,不等待然后报错
ping=0, # ping MySQL服务端,检查是否服务可用。# 如:0 = None = never, 1 = default = whenever it is requested, 2 = when a cursor is created, 4 = when a query is executed, 7 = always
host='127.0.0.1',
port=3306,
user='root',
password='222',
database='cmdb',
charset='utf8'
)
def fetchall(sql,*args):
""" 获取所有数据 """
conn = POOL.connection()
cursor = conn.cursor()
cursor.execute(sql,args)
result = cursor.fetchall()
cursor.close()
conn.close()
return result
def fetchone(sql, *args):
""" 获取单挑数据 """
conn = POOL.connection()
cursor = conn.cursor()
cursor.execute(sql, args)
result = cursor.fetchone()
cursor.close()
conn.close()
return result
from flask import Flask
import sqlhelper
app = Flask(__name__)
@app.route('/login')
def login():
result = sqlhelper.fetchone('select * from web_models_admininfo where username=%s ','wupeiqi')
print(result)
return 'login'
@app.route('/index')
def index():
result = sqlhelper.fetchall('select * from web_models_disk')
print(result)
return 'xxx'
@app.route('/order')
def order():
# result = fetchall('select * from user')
return 'xxx'
if __name__ == '__main__':
app.run()
基于类实现sqlhelper
import pymysql
from DBUtils.PooledDB import PooledDB
class SqlHelper(object):
def __init__(self):
self.pool = PooledDB(
creator=pymysql, # 使用链接数据库的模块
maxconnections=6, # 连接池允许的最大连接数,0和None表示不限制连接数
mincached=2, # 初始化时,链接池中至少创建的链接,0表示不创建
blocking=True, # 连接池中如果没有可用连接后,是否阻塞等待。True,等待;False,不等待然后报错
ping=0,
# ping MySQL服务端,检查是否服务可用。# 如:0 = None = never, 1 = default = whenever it is requested, 2 = when a cursor is created, 4 = when a query is executed, 7 = always
host='127.0.0.1',
port=3306,
user='root',
password='222',
database='cmdb',
charset='utf8'
)
def open(self):
conn = self.pool.connection()
cursor = conn.cursor()
return conn,cursor
def close(self,cursor,conn):
cursor.close()
conn.close()
def fetchall(self,sql, *args):
""" 获取所有数据 """
conn,cursor = self.open()
cursor.execute(sql, args)
result = cursor.fetchall()
self.close(conn,cursor)
return result
def fetchone(self,sql, *args):
""" 获取所有数据 """
conn, cursor = self.open()
cursor.execute(sql, args)
result = cursor.fetchone()
self.close(conn, cursor)
return result
db = SqlHelper()
from flask import Flask
from sqlhelper2 import db
app = Flask(__name__)
@app.route('/login')
def login():
# db.fetchone()
return 'login'
@app.route('/index')
def index():
# db.fetchall()
return 'xxx'
@app.route('/order')
def order():
# db.fetchall()
conn,cursor = db.open()
# 自己做操作
db.close(conn,cursor)
return 'xxx'
if __name__ == '__main__':
app.run()
2.补充知识点:上下文管理
class Foo(object):
def __enter__(self):
return 123
def __exit__(self, exc_type, exc_val, exc_tb):
pass
obj = Foo()
with obj as f:
print(f)
class Foo(object):
def do_somthing(self):
pass
def close(self):
pass
class Context:
def __enter__(self):
self.data = Foo()
return self.data
def __exit__(self, exc_type, exc_val, exc_tb):
self.data.close()
with Context() as ctx:
ctx.do_somthing()