fix: 修复 scheduler.py 和 sim_trade.py 中智能引擎调用连接泄漏
- scheduler.py: job_morning_trade/job_afternoon_trade 智能引擎 put_db 移到 finally - sim_trade.py: trigger_trade 降级路径 put_db 移到 finally,初始化 conn=None - 统一使用 smart_conn 变量名避免与外层 conn 冲突
This commit is contained in:
@@ -648,15 +648,22 @@ def trigger_trade():
|
|||||||
# 降级: 使用旧引擎
|
# 降级: 使用旧引擎
|
||||||
from services.scheduler import execute_auto_trade_for_user
|
from services.scheduler import execute_auto_trade_for_user
|
||||||
|
|
||||||
|
conn = None
|
||||||
|
try:
|
||||||
conn = get_db()
|
conn = get_db()
|
||||||
if conn:
|
if conn:
|
||||||
cur = conn.cursor(cursor_factory=RealDictCursor)
|
cur = conn.cursor(cursor_factory=RealDictCursor)
|
||||||
cur.execute("SELECT trade_quantity FROM sim_config WHERE user_id = %s", (user_id,))
|
cur.execute("SELECT trade_quantity FROM sim_config WHERE user_id = %s", (user_id,))
|
||||||
config = cur.fetchone()
|
config = cur.fetchone()
|
||||||
trade_quantity = config['trade_quantity'] if config else 1000
|
trade_quantity = config['trade_quantity'] if config else 1000
|
||||||
put_db(conn)
|
|
||||||
else:
|
else:
|
||||||
trade_quantity = 1000
|
trade_quantity = 1000
|
||||||
|
except Exception as e:
|
||||||
|
print(f"[trigger_trade] 获取交易配置失败: {e}")
|
||||||
|
trade_quantity = 1000
|
||||||
|
finally:
|
||||||
|
if conn:
|
||||||
|
put_db(conn)
|
||||||
|
|
||||||
result = execute_auto_trade_for_user(user_id, trade_quantity)
|
result = execute_auto_trade_for_user(user_id, trade_quantity)
|
||||||
|
|
||||||
|
|||||||
@@ -514,19 +514,22 @@ def job_morning_trade():
|
|||||||
for user in users:
|
for user in users:
|
||||||
user_id = user['user_id']
|
user_id = user['user_id']
|
||||||
# 尝试使用智能引擎
|
# 尝试使用智能引擎
|
||||||
|
smart_conn = None
|
||||||
try:
|
try:
|
||||||
from services.smart_trade_engine import execute_smart_trade
|
from services.smart_trade_engine import execute_smart_trade
|
||||||
from db import get_db, put_db
|
from db import get_db, put_db
|
||||||
conn = get_db()
|
smart_conn = get_db()
|
||||||
if conn:
|
if smart_conn:
|
||||||
result = execute_smart_trade(conn, user_id, scan_date=None)
|
result = execute_smart_trade(smart_conn, user_id, scan_date=None)
|
||||||
put_db(conn)
|
|
||||||
if result.get('success'):
|
if result.get('success'):
|
||||||
print(f"[定时任务] 用户{user_id} 智能引擎执行成功 "
|
print(f"[定时任务] 用户{user_id} 智能引擎执行成功 "
|
||||||
f"(算法:{result.get('algo','?')}, 信号:{result.get('signals',0)})")
|
f"(算法:{result.get('algo','?')}, 信号:{result.get('signals',0)})")
|
||||||
continue
|
continue
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f"[定时任务] 用户{user_id} 智能引擎异常,降级到旧引擎: {e}")
|
print(f"[定时任务] 用户{user_id} 智能引擎异常,降级到旧引擎: {e}")
|
||||||
|
finally:
|
||||||
|
if smart_conn:
|
||||||
|
put_db(smart_conn)
|
||||||
|
|
||||||
# 降级:使用旧引擎
|
# 降级:使用旧引擎
|
||||||
trade_quantity = user.get('trade_quantity') or 1000
|
trade_quantity = user.get('trade_quantity') or 1000
|
||||||
@@ -553,18 +556,21 @@ def job_afternoon_trade():
|
|||||||
for user in users:
|
for user in users:
|
||||||
user_id = user['user_id']
|
user_id = user['user_id']
|
||||||
# 尝试使用智能引擎
|
# 尝试使用智能引擎
|
||||||
|
smart_conn = None
|
||||||
try:
|
try:
|
||||||
from services.smart_trade_engine import execute_smart_trade
|
from services.smart_trade_engine import execute_smart_trade
|
||||||
from db import get_db, put_db
|
from db import get_db, put_db
|
||||||
conn = get_db()
|
smart_conn = get_db()
|
||||||
if conn:
|
if smart_conn:
|
||||||
result = execute_smart_trade(conn, user_id, scan_date=today)
|
result = execute_smart_trade(smart_conn, user_id, scan_date=today)
|
||||||
put_db(conn)
|
|
||||||
if result.get('success'):
|
if result.get('success'):
|
||||||
print(f"[定时任务] 用户{user_id} 午后智能引擎执行成功")
|
print(f"[定时任务] 用户{user_id} 午后智能引擎执行成功")
|
||||||
continue
|
continue
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f"[定时任务] 用户{user_id} 智能引擎异常,降级: {e}")
|
print(f"[定时任务] 用户{user_id} 智能引擎异常,降级: {e}")
|
||||||
|
finally:
|
||||||
|
if smart_conn:
|
||||||
|
put_db(smart_conn)
|
||||||
trade_quantity = user.get('trade_quantity') or 1000
|
trade_quantity = user.get('trade_quantity') or 1000
|
||||||
execute_auto_trade_for_user(user_id, trade_quantity, scan_date=today)
|
execute_auto_trade_for_user(user_id, trade_quantity, scan_date=today)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user