cxhy 6 mesi fa
parent
commit
883c93593c
3 ha cambiato i file con 18 aggiunte e 1 eliminazioni
  1. 12 0
      db_decorator.py
  2. 4 0
      main.py
  3. 2 1
      model.py

+ 12 - 0
db_decorator.py

@@ -0,0 +1,12 @@
+
+
+# peewee db close 装饰器
+from model import ZaneTest
+
+
+def peewee_db_close(func):
+    async def wrapper():
+        # print(args)
+        ZaneTest._schema.database.close()
+        return await func()
+    return wrapper

+ 4 - 0
main.py

@@ -11,6 +11,7 @@ from pymysql import OperationalError
 from starlette.middleware.cors import CORSMiddleware
 
 from LocalModel import CustomLogin, SaveUser, QueryUser, DeleteUser
+from db_decorator import peewee_db_close
 from logic import *
 from model import CustomUser, UserInfo, ZaneTest, database
 
@@ -312,7 +313,10 @@ async def test(request: Request):
 
 
 @app.get("/api/test2")
+@peewee_db_close
 async def test2():
+
     zane_list = ZaneTest.select().where(ZaneTest.a != None)
     zane_list = [dt.a for dt in zane_list]
+
     return {"message": "Hello World", "users": zane_list}

+ 2 - 1
model.py

@@ -7,7 +7,8 @@ database = PooledMySQLDatabase('yixue_test',
                                host='test.db.cxhy.cn',
                                port=3307,
                                max_connections=32,
-                               stale_timeout=5, )
+                               stale_timeout=5,
+                               timeout=5)
 
 
 class UnknownField(object):