|
|
@@ -4,26 +4,23 @@ import threading
|
|
|
import time
|
|
|
import atexit
|
|
|
from collections import deque
|
|
|
-
|
|
|
-
|
|
|
-# simple direct connector (used by some scripts)
|
|
|
from pymysql.cursors import DictCursor
|
|
|
|
|
|
-def get_connection():
|
|
|
- return pymysql.connect(
|
|
|
- host="120.24.26.108",
|
|
|
- port=3307,
|
|
|
- user="root",
|
|
|
- password="zhijiayun123456",
|
|
|
- database="drug_retrieve",
|
|
|
- charset="utf8mb4",
|
|
|
- autocommit=False,
|
|
|
- cursorclass=DictCursor,
|
|
|
- )
|
|
|
-
|
|
|
-
|
|
|
-# connection pool used by a_mt.py
|
|
|
+# ============================================================
|
|
|
+# 数据库配置
|
|
|
+# ============================================================
|
|
|
+DB_HOST = "120.24.26.108"
|
|
|
+DB_PORT = 3307
|
|
|
+DB_USER = "collect_user"
|
|
|
+DB_PASSWORD = "collect123456"
|
|
|
+DB_NAME = "drug_retrieve" # 正式库;测试库改 drug_retrieve_test
|
|
|
+DB_CHARSET = "utf8mb4"
|
|
|
+
|
|
|
+# ============================================================
|
|
|
+# 连接池
|
|
|
+# ============================================================
|
|
|
MYSQL_POOL = None
|
|
|
+POOL_LOCK = threading.Lock()
|
|
|
|
|
|
|
|
|
class PooledConnection:
|
|
|
@@ -137,39 +134,49 @@ class MySQLConnectionPool:
|
|
|
self._total = 0
|
|
|
|
|
|
|
|
|
+def _pool_kwargs():
|
|
|
+ """连接池参数"""
|
|
|
+ return {
|
|
|
+ "host": DB_HOST,
|
|
|
+ "port": DB_PORT,
|
|
|
+ "user": DB_USER,
|
|
|
+ "password": DB_PASSWORD,
|
|
|
+ "database": DB_NAME,
|
|
|
+ "charset": DB_CHARSET,
|
|
|
+ "cursorclass": pymysql.cursors.Cursor,
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
def get_mysql():
|
|
|
+ """获取数据库连接(优先连接池,失败则直连)"""
|
|
|
global MYSQL_POOL
|
|
|
- if 'MYSQL_POOL' not in globals() or MYSQL_POOL is None:
|
|
|
- MYSQL_POOL = None
|
|
|
- try:
|
|
|
- MYSQL_POOL = MySQLConnectionPool(
|
|
|
- minconn=1,
|
|
|
- maxconn=20,
|
|
|
- host="120.24.26.108",
|
|
|
- port=3307,
|
|
|
- user="root",
|
|
|
- password="zhijiayun123456",
|
|
|
- database="drug_retrieve",
|
|
|
- charset="utf8mb4",
|
|
|
- cursorclass=pymysql.cursors.Cursor,
|
|
|
- )
|
|
|
- except Exception:
|
|
|
- MYSQL_POOL = None
|
|
|
+ with POOL_LOCK:
|
|
|
+ if MYSQL_POOL is None:
|
|
|
+ try:
|
|
|
+ MYSQL_POOL = MySQLConnectionPool(minconn=1, maxconn=20, **_pool_kwargs())
|
|
|
+ except Exception:
|
|
|
+ MYSQL_POOL = None
|
|
|
if MYSQL_POOL is None:
|
|
|
- return pymysql.connect(
|
|
|
- host="120.24.26.108",
|
|
|
- port=3307,
|
|
|
- user="root",
|
|
|
- password="zhijiayun123456",
|
|
|
- database="drug_retrieve",
|
|
|
- charset="utf8mb4",
|
|
|
- cursorclass=pymysql.cursors.Cursor,
|
|
|
- )
|
|
|
+ return pymysql.connect(**_pool_kwargs())
|
|
|
return MYSQL_POOL.getconn()
|
|
|
|
|
|
|
|
|
+def get_connection():
|
|
|
+ """直连数据库(兼容旧脚本,DictCursor 模式)"""
|
|
|
+ return pymysql.connect(
|
|
|
+ host=DB_HOST,
|
|
|
+ port=DB_PORT,
|
|
|
+ user=DB_USER,
|
|
|
+ password=DB_PASSWORD,
|
|
|
+ database=DB_NAME,
|
|
|
+ charset=DB_CHARSET,
|
|
|
+ autocommit=False,
|
|
|
+ cursorclass=DictCursor,
|
|
|
+ )
|
|
|
+
|
|
|
+
|
|
|
# 在程序退出时确保池中所有物理连接被关闭
|
|
|
try:
|
|
|
- atexit.register(lambda: MYSQL_POOL.closeall() if 'MYSQL_POOL' in globals() and MYSQL_POOL is not None else None)
|
|
|
+ atexit.register(lambda: MYSQL_POOL.closeall() if MYSQL_POOL is not None else None)
|
|
|
except Exception:
|
|
|
- pass
|
|
|
+ pass
|