2
0

2 Коммиты 6af17de2c1 ... 6c44a9b781

Автор SHA1 Сообщение Дата
  cxhy 6c44a9b781 1 6 месяцев назад
  cxhy 883c93593c 1 6 месяцев назад
3 измененных файлов с 30 добавлено и 4 удалено
  1. 12 0
      db_decorator.py
  2. 4 0
      main.py
  3. 14 4
      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}

+ 14 - 4
model.py

@@ -1,13 +1,23 @@
 from peewee import *
 from playhouse.pool import PooledMySQLDatabase
 
-database = PooledMySQLDatabase('yixue_test',
-                               user='yixue_test',
+# database = PooledMySQLDatabase('yixue_test',
+#                                user='yixue_test',
+#                                password='Lai123',
+#                                host='test.db.cxhy.cn',
+#                                port=3307,
+#                                max_connections=32,
+#                                stale_timeout=5,
+#                                timeout=5)
+
+database = PooledMySQLDatabase('yixue',
+                               user='yixue',
                                password='Lai123',
                                host='test.db.cxhy.cn',
-                               port=3307,
+                               port=3306,
                                max_connections=32,
-                               stale_timeout=5, )
+                               stale_timeout=5,
+                               timeout=5)
 
 
 class UnknownField(object):