fix: 修复连接泄漏、double-put_db、北交所映射缺失
- analysis.py: 6个端点将 put_db(conn) 从 try 移到 finally,异常时不泄漏 - trades.py: 移除 update_trade/delete_trade 中多余的 put_db(conn),避免 double-put 破坏连接池 - trades.py: stoploss_check 补充北交所(8/9开头)腾讯API代码映射
This commit is contained in:
@@ -601,9 +601,10 @@ def batch_technical_signals():
|
|||||||
scan_map[row['code']] = row
|
scan_map[row['code']] = row
|
||||||
|
|
||||||
cur.close()
|
cur.close()
|
||||||
put_db(conn)
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f"批量扫描获取数据失败: {e}")
|
print(f"批量扫描获取数据失败: {e}")
|
||||||
|
finally:
|
||||||
|
put_db(conn)
|
||||||
|
|
||||||
results = []
|
results = []
|
||||||
errors = []
|
errors = []
|
||||||
@@ -919,7 +920,6 @@ def get_scan_results():
|
|||||||
recommend_counts[disp] = recommend_counts.get(disp, 0) + 1
|
recommend_counts[disp] = recommend_counts.get(disp, 0) + 1
|
||||||
|
|
||||||
cur.close()
|
cur.close()
|
||||||
put_db(conn)
|
|
||||||
|
|
||||||
return jsonify({
|
return jsonify({
|
||||||
'success': True,
|
'success': True,
|
||||||
@@ -939,6 +939,8 @@ def get_scan_results():
|
|||||||
})
|
})
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
return jsonify({'success': False, 'error': str(e)}), 500
|
return jsonify({'success': False, 'error': str(e)}), 500
|
||||||
|
finally:
|
||||||
|
put_db(conn)
|
||||||
|
|
||||||
|
|
||||||
def _compute_recommend(signal_status, indicators, triggered_count, is_holding):
|
def _compute_recommend(signal_status, indicators, triggered_count, is_holding):
|
||||||
@@ -994,7 +996,6 @@ def signal_alerts():
|
|||||||
WHERE code IN ({placeholders})
|
WHERE code IN ({placeholders})
|
||||||
""", stock_codes)
|
""", stock_codes)
|
||||||
price_rows = cur.fetchall()
|
price_rows = cur.fetchall()
|
||||||
put_db(conn)
|
|
||||||
|
|
||||||
price_map = {}
|
price_map = {}
|
||||||
change_map = {}
|
change_map = {}
|
||||||
@@ -1076,6 +1077,8 @@ def signal_alerts():
|
|||||||
import traceback
|
import traceback
|
||||||
traceback.print_exc()
|
traceback.print_exc()
|
||||||
return jsonify({'success': False, 'error': str(e)}), 500
|
return jsonify({'success': False, 'error': str(e)}), 500
|
||||||
|
finally:
|
||||||
|
put_db(conn)
|
||||||
|
|
||||||
|
|
||||||
def _is_scan_running():
|
def _is_scan_running():
|
||||||
@@ -1140,7 +1143,6 @@ def get_scan_status():
|
|||||||
triggered = cur.fetchone()[0]
|
triggered = cur.fetchone()[0]
|
||||||
|
|
||||||
cur.close()
|
cur.close()
|
||||||
put_db(conn)
|
|
||||||
|
|
||||||
return jsonify({
|
return jsonify({
|
||||||
'success': True,
|
'success': True,
|
||||||
@@ -1154,6 +1156,8 @@ def get_scan_status():
|
|||||||
})
|
})
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
return jsonify({'success': False, 'error': str(e)}), 500
|
return jsonify({'success': False, 'error': str(e)}), 500
|
||||||
|
finally:
|
||||||
|
put_db(conn)
|
||||||
|
|
||||||
|
|
||||||
@bp.route('/start_full_scan', methods=['POST'])
|
@bp.route('/start_full_scan', methods=['POST'])
|
||||||
@@ -1225,7 +1229,6 @@ def get_scan_strategy():
|
|||||||
""", (scan_date,))
|
""", (scan_date,))
|
||||||
rows = cur.fetchall()
|
rows = cur.fetchall()
|
||||||
cur.close()
|
cur.close()
|
||||||
put_db(conn)
|
|
||||||
|
|
||||||
tier1, tier2, tier3, tier4 = [], [], [], []
|
tier1, tier2, tier3, tier4 = [], [], [], []
|
||||||
for r in rows:
|
for r in rows:
|
||||||
@@ -1306,6 +1309,8 @@ def get_scan_strategy():
|
|||||||
})
|
})
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
return jsonify({'success': False, 'error': str(e)}), 500
|
return jsonify({'success': False, 'error': str(e)}), 500
|
||||||
|
finally:
|
||||||
|
put_db(conn)
|
||||||
|
|
||||||
|
|
||||||
def _get_kline_data(stock_code, days=120):
|
def _get_kline_data(stock_code, days=120):
|
||||||
@@ -1357,8 +1362,6 @@ def get_bull_stocks():
|
|||||||
for p in cur.fetchall():
|
for p in cur.fetchall():
|
||||||
price_map[p['code']] = {'price': float(p['price']), 'change_pct': float(p.get('change_pct') or 0)}
|
price_map[p['code']] = {'price': float(p['price']), 'change_pct': float(p.get('change_pct') or 0)}
|
||||||
|
|
||||||
put_db(conn)
|
|
||||||
|
|
||||||
# ---- 批量计算综合评分 ----
|
# ---- 批量计算综合评分 ----
|
||||||
scores_map = None
|
scores_map = None
|
||||||
try:
|
try:
|
||||||
@@ -1420,6 +1423,8 @@ def get_bull_stocks():
|
|||||||
import traceback
|
import traceback
|
||||||
traceback.print_exc()
|
traceback.print_exc()
|
||||||
return jsonify({'success': False, 'error': str(e)}), 500
|
return jsonify({'success': False, 'error': str(e)}), 500
|
||||||
|
finally:
|
||||||
|
put_db(conn)
|
||||||
|
|
||||||
|
|
||||||
def _llm_polish_summary(stock_name, stock_code, ai_summary, score, verdict):
|
def _llm_polish_summary(stock_name, stock_code, ai_summary, score, verdict):
|
||||||
|
|||||||
@@ -130,7 +130,6 @@ def update_trade(trade_id):
|
|||||||
""", (trade_id, user_id))
|
""", (trade_id, user_id))
|
||||||
old_trade = cur.fetchone()
|
old_trade = cur.fetchone()
|
||||||
if not old_trade:
|
if not old_trade:
|
||||||
put_db(conn)
|
|
||||||
return jsonify({'error': '交易记录不存在'}), 404
|
return jsonify({'error': '交易记录不存在'}), 404
|
||||||
|
|
||||||
cur.execute("""
|
cur.execute("""
|
||||||
@@ -152,7 +151,6 @@ def update_trade(trade_id):
|
|||||||
trade = cur.fetchone()
|
trade = cur.fetchone()
|
||||||
if not trade:
|
if not trade:
|
||||||
conn.rollback()
|
conn.rollback()
|
||||||
put_db(conn)
|
|
||||||
return jsonify({'error': '交易记录不存在'}), 404
|
return jsonify({'error': '交易记录不存在'}), 404
|
||||||
|
|
||||||
old_delta = _calc_cash_delta(old_trade.get('trade_type'), old_trade.get('price'), old_trade.get('quantity'))
|
old_delta = _calc_cash_delta(old_trade.get('trade_type'), old_trade.get('price'), old_trade.get('quantity'))
|
||||||
@@ -194,7 +192,6 @@ def delete_trade(trade_id):
|
|||||||
""", (trade_id, user_id))
|
""", (trade_id, user_id))
|
||||||
old_trade = cur.fetchone()
|
old_trade = cur.fetchone()
|
||||||
if not old_trade:
|
if not old_trade:
|
||||||
put_db(conn)
|
|
||||||
return jsonify({'success': False, 'error': '交易记录不存在'}), 404
|
return jsonify({'success': False, 'error': '交易记录不存在'}), 404
|
||||||
|
|
||||||
cur.execute("DELETE FROM trades WHERE id = %s AND user_id = %s", (trade_id, user_id))
|
cur.execute("DELETE FROM trades WHERE id = %s AND user_id = %s", (trade_id, user_id))
|
||||||
@@ -306,7 +303,7 @@ def check_stoploss():
|
|||||||
current_price = 0
|
current_price = 0
|
||||||
try:
|
try:
|
||||||
import requests as _rq
|
import requests as _rq
|
||||||
_tc = ('sh' if code.startswith('6') else 'sz') + code
|
_tc = ('sh' if code.startswith('6') else 'bj' if code.startswith(('8', '9')) else 'sz') + code
|
||||||
_rr = _rq.get(f'http://qt.gtimg.cn/q={_tc}', timeout=5,
|
_rr = _rq.get(f'http://qt.gtimg.cn/q={_tc}', timeout=5,
|
||||||
headers={'Referer': 'https://finance.qq.com'})
|
headers={'Referer': 'https://finance.qq.com'})
|
||||||
if _rr.status_code == 200 and '\"' in _rr.text:
|
if _rr.status_code == 200 and '\"' in _rr.text:
|
||||||
|
|||||||
Reference in New Issue
Block a user