perf(database): 优化数据库连接池配置

- 添加 pool_recycle 和 pool_pre_ping 参数,提高连接池性能和稳定性
- 修改 get_session 函数,确保正确返回 sessionmaker 对象
- 移除不必要的 print 语句,简化日志输出
- 优化心跳检测逻辑,提高系统稳定性
This commit is contained in:
danial
2025-01-29 20:57:09 +08:00
parent 4d29b89f2d
commit 29494a9fdd
4 changed files with 6 additions and 5 deletions

View File

@@ -181,9 +181,7 @@ def run_task():
"""
# 注册信号处理程序
has_been_console = False
master_node_service = MasterNodeService()
while True:
print(f"{datetime.now().strftime('%Y-%m-%d %H:%M:%S')}\t当前开始运行")
master_node_service = MasterNodeService()
# 1s获取两次订单
master_order = master_node_service.query_order()
@@ -196,7 +194,6 @@ def run_task():
print(f"{datetime.now().strftime('%Y-%m-%d %H:%M:%S')}\t暂时没有订单")
if datetime.now().second % 10 != 0 and has_been_console:
has_been_console = False
print(f"{datetime.now().strftime('%Y-%m-%d %H:%M:%S')}\t当前运行完成")
time.sleep(1)
@@ -211,7 +208,7 @@ def run():
# 10分钟打印一次信息
if datetime.now().second % 30 == 0 and not has_been_console:
has_been_console = True
print(f"{datetime.now().strftime('%Y-%m-%d %H:%M:%S')}\t心跳正常 鲜橙状态:{process.is_alive()}",
print(f"{datetime.now().strftime('%Y-%m-%d %H:%M:%S')}\t心跳正常 当前状态:{process.is_alive()}",
flush=True, )
if datetime.now().second % 30 != 0 and has_been_console:
has_been_console = False

View File

@@ -7,4 +7,6 @@ engine = create_engine(
pool_size=100,
echo_pool=True,
echo=True,
pool_recycle=300, # 每隔 300 秒5分钟强制回收连接
pool_pre_ping=True # 每次从连接池取连接前执行简单 ping 测试
)

View File

@@ -48,4 +48,5 @@ Base.metadata.create_all(engine)
def get_session() -> sessionmaker[Session]:
# 创建会话工厂
return sessionmaker(bind=engine)
session= sessionmaker(bind=engine)
return session

View File

@@ -1,6 +1,7 @@
import time
import uuid
import pymysql
from loguru import logger
from src.integrations.itunes.api import AppleClient