From 5f5fa7ae8019cafd6292b5867749241c82dee568 Mon Sep 17 00:00:00 2001 From: freedakgmail Date: Wed, 22 Jul 2026 07:31:21 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E8=BF=9E=E6=8E=A5?= =?UTF-8?q?=E6=B3=84=E6=BC=8F=E3=80=81double-put=5Fdb=E3=80=81=E5=8C=97?= =?UTF-8?q?=E4=BA=A4=E6=89=80=E6=98=A0=E5=B0=84=E7=BC=BA=E5=A4=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 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代码映射 --- stock-html/routes/analysis.py | 19 ++++++++++++------- stock-html/routes/trades.py | 5 +---- 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/stock-html/routes/analysis.py b/stock-html/routes/analysis.py index b588f2d..65b0bdd 100644 --- a/stock-html/routes/analysis.py +++ b/stock-html/routes/analysis.py @@ -601,9 +601,10 @@ def batch_technical_signals(): scan_map[row['code']] = row cur.close() - put_db(conn) except Exception as e: print(f"批量扫描获取数据失败: {e}") + finally: + put_db(conn) results = [] errors = [] @@ -919,7 +920,6 @@ def get_scan_results(): recommend_counts[disp] = recommend_counts.get(disp, 0) + 1 cur.close() - put_db(conn) return jsonify({ 'success': True, @@ -939,6 +939,8 @@ def get_scan_results(): }) except Exception as e: return jsonify({'success': False, 'error': str(e)}), 500 + finally: + put_db(conn) def _compute_recommend(signal_status, indicators, triggered_count, is_holding): @@ -994,7 +996,6 @@ def signal_alerts(): WHERE code IN ({placeholders}) """, stock_codes) price_rows = cur.fetchall() - put_db(conn) price_map = {} change_map = {} @@ -1076,6 +1077,8 @@ def signal_alerts(): import traceback traceback.print_exc() return jsonify({'success': False, 'error': str(e)}), 500 + finally: + put_db(conn) def _is_scan_running(): @@ -1140,7 +1143,6 @@ def get_scan_status(): triggered = cur.fetchone()[0] cur.close() - put_db(conn) return jsonify({ 'success': True, @@ -1154,6 +1156,8 @@ def get_scan_status(): }) except Exception as e: return jsonify({'success': False, 'error': str(e)}), 500 + finally: + put_db(conn) @bp.route('/start_full_scan', methods=['POST']) @@ -1225,7 +1229,6 @@ def get_scan_strategy(): """, (scan_date,)) rows = cur.fetchall() cur.close() - put_db(conn) tier1, tier2, tier3, tier4 = [], [], [], [] for r in rows: @@ -1306,6 +1309,8 @@ def get_scan_strategy(): }) except Exception as e: return jsonify({'success': False, 'error': str(e)}), 500 + finally: + put_db(conn) def _get_kline_data(stock_code, days=120): @@ -1357,8 +1362,6 @@ def get_bull_stocks(): for p in cur.fetchall(): price_map[p['code']] = {'price': float(p['price']), 'change_pct': float(p.get('change_pct') or 0)} - put_db(conn) - # ---- 批量计算综合评分 ---- scores_map = None try: @@ -1420,6 +1423,8 @@ def get_bull_stocks(): import traceback traceback.print_exc() return jsonify({'success': False, 'error': str(e)}), 500 + finally: + put_db(conn) def _llm_polish_summary(stock_name, stock_code, ai_summary, score, verdict): diff --git a/stock-html/routes/trades.py b/stock-html/routes/trades.py index 25d6a26..3085c51 100644 --- a/stock-html/routes/trades.py +++ b/stock-html/routes/trades.py @@ -130,7 +130,6 @@ def update_trade(trade_id): """, (trade_id, user_id)) old_trade = cur.fetchone() if not old_trade: - put_db(conn) return jsonify({'error': '交易记录不存在'}), 404 cur.execute(""" @@ -152,7 +151,6 @@ def update_trade(trade_id): trade = cur.fetchone() if not trade: conn.rollback() - put_db(conn) return jsonify({'error': '交易记录不存在'}), 404 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)) old_trade = cur.fetchone() if not old_trade: - put_db(conn) return jsonify({'success': False, 'error': '交易记录不存在'}), 404 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 try: 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, headers={'Referer': 'https://finance.qq.com'}) if _rr.status_code == 200 and '\"' in _rr.text: