From a8737f1ec89c8daad907ee8b0808df0335b5d3e3 Mon Sep 17 00:00:00 2001 From: wzy-warehouse <18135009705@163.com> Date: Sat, 6 Jun 2026 13:34:18 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=95=B0=E6=8D=AE=E5=BA=93?= =?UTF-8?q?=E8=BF=9E=E6=8E=A5=E6=B1=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/models/dbn/earthquake/earthquake_dbn.py | 4 +-- app/models/dbn/rainfall/rainfall_dbn.py | 2 +- app/utils/db_helper.py | 38 +++++++++++++-------- 3 files changed, 25 insertions(+), 19 deletions(-) diff --git a/app/models/dbn/earthquake/earthquake_dbn.py b/app/models/dbn/earthquake/earthquake_dbn.py index 0e64d51..5d6e829 100644 --- a/app/models/dbn/earthquake/earthquake_dbn.py +++ b/app/models/dbn/earthquake/earthquake_dbn.py @@ -218,7 +218,7 @@ class EarthquakeDBN: lat = point.get('lat') source_type = point.get('source_type') - logger.info(f"地震预测点 ID={point_id}, source_type={source_type}") + logger.debug(f"地震预测点 ID={point_id}, source_type={source_type}") # 计算震中距(如果未直接提供) if epicenter_distance is None: @@ -226,7 +226,6 @@ class EarthquakeDBN: epicenter_distance = self._haversine_distance( lon, lat, epicenter_lon, epicenter_lat ) - logger.info(f"计算震中距: {epicenter_distance:.1f} km") else: logger.warning("未提供震中距或震中坐标,使用默认值 100km") epicenter_distance = 100.0 @@ -234,7 +233,6 @@ class EarthquakeDBN: # 估算地震烈度(如果未直接提供) if seismic_intensity is None: seismic_intensity = self.estimate_seismic_intensity(magnitude, epicenter_distance, depth) - logger.info(f"估算地震烈度: {seismic_intensity:.1f}") # 获取静态因子数据 raw_factors = point.get('static_factors', {}) diff --git a/app/models/dbn/rainfall/rainfall_dbn.py b/app/models/dbn/rainfall/rainfall_dbn.py index 47666b5..b9d970d 100644 --- a/app/models/dbn/rainfall/rainfall_dbn.py +++ b/app/models/dbn/rainfall/rainfall_dbn.py @@ -234,7 +234,7 @@ class RainfallDBN: lat = point.get('lat') source_type = point.get('source_type') - logger.info(f"预测点 ID={point_id}, source_type={source_type}") + logger.debug(f"预测点 ID={point_id}, source_type={source_type}") # 获取降雨数据 if rainfall is not None and duration is not None: diff --git a/app/utils/db_helper.py b/app/utils/db_helper.py index 9a9647c..118f134 100644 --- a/app/utils/db_helper.py +++ b/app/utils/db_helper.py @@ -1,8 +1,9 @@ """ PostgreSQL 数据库工具类 -提供增删改查方法 +提供增删改查方法,内置连接池 """ import psycopg2 +from psycopg2 import pool from psycopg2.extras import RealDictCursor from typing import List, Dict, Any, Optional, Tuple from contextlib import contextmanager @@ -10,10 +11,10 @@ from config import settings class PostgresSQLHelper: - """PostgreSQL 数据库帮助类""" - + """PostgreSQL 数据库帮助类(连接池版)""" + def __init__(self): - """初始化数据库连接配置""" + """初始化数据库连接池""" self.db_config = { 'host': settings.DB_HOST, 'port': settings.DB_PORT, @@ -21,31 +22,38 @@ class PostgresSQLHelper: 'password': settings.DB_PASSWORD, 'database': settings.DB_NAME, } - + self._pool = None + + def _ensure_pool(self): + """延迟初始化连接池""" + if self._pool is None: + self._pool = pool.ThreadedConnectionPool( + minconn=2, + maxconn=20, + **self.db_config + ) + @contextmanager def get_connection(self): """ - 获取数据库连接的上下文管理器 - 自动管理连接的开启和关闭 + 从连接池获取连接(复用TCP连接,省去握手开销) """ - conn = None + self._ensure_pool() + conn = self._pool.getconn() try: - conn = psycopg2.connect(**self.db_config) yield conn conn.commit() except Exception as e: - if conn: - conn.rollback() + conn.rollback() raise e finally: - if conn: - conn.close() - + self._pool.putconn(conn) + @contextmanager def get_cursor(self, dict_cursor=False): """ 获取数据库游标的上下文管理器 - + Args: dict_cursor: 是否使用字典游标(返回字典格式结果) """