尽管这里有很多答案,但我还是无法正确理解我的问题
我的app.py
的样子如下:
from flask import Flask, request, jsonify,session
from flask_cors import CORS
from dbconfig import employee_details,Emps_schema,Emp_schema,db
app=Flask(__name__)
app.secret_key = 'the random string'
CORS(app)
@app.route("/addemp", methods=["POST"])
def add_emp():
empname = request.json['empname']
email = request.json['email']
department = request.json['department']
new_emp = employee_details(empname, email, department,password='***')
db.session.add(new_emp)
db.session.commit()
return Emp_schema.jsonify(new_emp)
@app.route("/emp/<empid>", methods=["PUT"])
def employee_update(empid):
emp = employee_details.query.get(empid)
empname = request.json['empname']
department = request.json['department']
emp.empname=empname
emp.department=department
db.session.commit()
return Emp_schema.jsonify(emp)
如果我尝试关闭会话,则update [PUT]不起作用(根据许多答案,每次提交后都应关闭会话)
和
dbconfig.py
:
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask_marshmallow import Marshmallow
app=Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://root:root@localhost/empdb'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS']=False
# app.config['SQLALCHEMY_POOL_SIZE'] = 30
app.config["SQLALCHEMY_ENGINE_OPTIONS"] = {"pool_size": 20}
db = SQLAlchemy(app)
ma=Marshmallow(app)
class employee_details(db.Model):
empid = db.Column(db.Integer,primary_key=True)
empname = db.Column(db.String(20), unique=False)
email = db.Column(db.String(50))
department = db.Column(db.String(50))
password = db.Column(db.String(100))
def __init__(self,empname,email,department,password):
self.empname=empname
self.email=email
self.department=department
self.password=password
class EmpSchema(ma.Schema):
class Meta:
fields = ('empid','empname','department',,'email')
Emp_schema = EmpSchema()
Emps_schema = EmpSchema(many=True)
db.create_all()
我该如何解决此错误:
sqlalchemy.exc.TimeoutError: QueuePool limit of size 10 overflow 10 reached, connection timed out, timeout 30
是否对我的代码进行了修改以解决此问题?请提出建议