fix: 修复 sim_trade/smart_trade 未导入 put_db 导致 NameError 和连接泄漏
- sim_trade.py: 导入 put_db(之前使用了但未导入,每次 finally 都会 NameError) - smart_trade.py: 同上,9处 put_db 调用因未导入而失效 - sim_trade.py: trigger_trade 智能引擎异常时 conn 泄漏,改用 finally 确保归还
This commit is contained in:
@@ -4,7 +4,7 @@
|
||||
"""
|
||||
from flask import Blueprint, request, jsonify
|
||||
from datetime import datetime, date, time
|
||||
from db import get_db, login_required, get_current_user_id
|
||||
from db import get_db, put_db, login_required, get_current_user_id
|
||||
from psycopg2.extras import RealDictCursor
|
||||
|
||||
bp = Blueprint('sim_trade', __name__, url_prefix='/api/sim')
|
||||
@@ -623,12 +623,12 @@ def trigger_trade():
|
||||
|
||||
try:
|
||||
# 优先使用智能引擎
|
||||
smart_conn = None
|
||||
try:
|
||||
from services.smart_trade_engine import execute_smart_trade
|
||||
conn = get_db()
|
||||
if conn:
|
||||
result = execute_smart_trade(conn, user_id, scan_date=None)
|
||||
put_db(conn)
|
||||
smart_conn = get_db()
|
||||
if smart_conn:
|
||||
result = execute_smart_trade(smart_conn, user_id, scan_date=None)
|
||||
if result.get('success'):
|
||||
results = result.get('results', [])
|
||||
buy_count = len([r for r in results if r['type'] == 'buy'])
|
||||
@@ -641,6 +641,9 @@ def trigger_trade():
|
||||
})
|
||||
except Exception as e:
|
||||
print(f"[trigger_trade] 智能引擎异常,降级: {e}")
|
||||
finally:
|
||||
if smart_conn:
|
||||
put_db(smart_conn)
|
||||
|
||||
# 降级: 使用旧引擎
|
||||
from services.scheduler import execute_auto_trade_for_user
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
"""
|
||||
from flask import Blueprint, request, jsonify
|
||||
from datetime import date
|
||||
from db import get_db, login_required, get_current_user_id
|
||||
from db import get_db, put_db, login_required, get_current_user_id
|
||||
from psycopg2.extras import RealDictCursor
|
||||
|
||||
bp = Blueprint('smart_trade', __name__, url_prefix='/api/smart')
|
||||
|
||||
Reference in New Issue
Block a user