cxppython 0.2.9__tar.gz → 0.2.10__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {cxppython-0.2.9 → cxppython-0.2.10}/PKG-INFO +1 -1
- {cxppython-0.2.9 → cxppython-0.2.10}/cxppython/utils/database/mysql.py +47 -0
- {cxppython-0.2.9 → cxppython-0.2.10}/cxppython.egg-info/PKG-INFO +1 -1
- {cxppython-0.2.9 → cxppython-0.2.10}/setup.py +1 -1
- {cxppython-0.2.9 → cxppython-0.2.10}/LICENSE +0 -0
- {cxppython-0.2.9 → cxppython-0.2.10}/README.md +0 -0
- {cxppython-0.2.9 → cxppython-0.2.10}/cxppython/__init__.py +0 -0
- {cxppython-0.2.9 → cxppython-0.2.10}/cxppython/bittensor/__init__.py +0 -0
- {cxppython-0.2.9 → cxppython-0.2.10}/cxppython/bittensor/synapse.py +0 -0
- {cxppython-0.2.9 → cxppython-0.2.10}/cxppython/core/__init__.py +0 -0
- {cxppython-0.2.9 → cxppython-0.2.10}/cxppython/core/config.py +0 -0
- {cxppython-0.2.9 → cxppython-0.2.10}/cxppython/main.py +0 -0
- {cxppython-0.2.9 → cxppython-0.2.10}/cxppython/utils/__init__.py +0 -0
- {cxppython-0.2.9 → cxppython-0.2.10}/cxppython/utils/cxplogging/__init__.py +0 -0
- {cxppython-0.2.9 → cxppython-0.2.10}/cxppython/utils/cxplogging/console.py +0 -0
- {cxppython-0.2.9 → cxppython-0.2.10}/cxppython/utils/cxplogging/defines.py +0 -0
- {cxppython-0.2.9 → cxppython-0.2.10}/cxppython/utils/cxplogging/format.py +0 -0
- {cxppython-0.2.9 → cxppython-0.2.10}/cxppython/utils/cxplogging/helpers.py +0 -0
- {cxppython-0.2.9 → cxppython-0.2.10}/cxppython/utils/cxplogging/loggingmachine.py +0 -0
- {cxppython-0.2.9 → cxppython-0.2.10}/cxppython/utils/cxplogging/logginsimple.py +0 -0
- {cxppython-0.2.9 → cxppython-0.2.10}/cxppython/utils/database/__init__.py +0 -0
- {cxppython-0.2.9 → cxppython-0.2.10}/cxppython/utils/database/mongo_singleton.py +0 -0
- {cxppython-0.2.9 → cxppython-0.2.10}/cxppython/utils/database/mysql_singleton.py +0 -0
- {cxppython-0.2.9 → cxppython-0.2.10}/cxppython/utils/database/redis.py +0 -0
- {cxppython-0.2.9 → cxppython-0.2.10}/cxppython/utils/easy_imports.py +0 -0
- {cxppython-0.2.9 → cxppython-0.2.10}/cxppython.egg-info/SOURCES.txt +0 -0
- {cxppython-0.2.9 → cxppython-0.2.10}/cxppython.egg-info/dependency_links.txt +0 -0
- {cxppython-0.2.9 → cxppython-0.2.10}/cxppython.egg-info/requires.txt +0 -0
- {cxppython-0.2.9 → cxppython-0.2.10}/cxppython.egg-info/top_level.txt +0 -0
- {cxppython-0.2.9 → cxppython-0.2.10}/setup.cfg +0 -0
|
@@ -48,12 +48,59 @@ class MysqlDB:
|
|
|
48
48
|
self.async_engine: Optional[AsyncEngine] = None
|
|
49
49
|
self.async_session_factory: Optional[Any] = None
|
|
50
50
|
self._async_engine_created: bool = False
|
|
51
|
+
self._ctx_session: Optional[Session] = None
|
|
52
|
+
self._async_ctx_session: Optional[AsyncSession] = None
|
|
51
53
|
|
|
52
54
|
logger.info("MysqlDB 同步引擎已创建。")
|
|
53
55
|
|
|
54
56
|
if async_engine:
|
|
55
57
|
self._ensure_async_engine()
|
|
56
58
|
|
|
59
|
+
# --- 上下文管理协议,便于 with 使用 ---
|
|
60
|
+
def __enter__(self) -> Session:
|
|
61
|
+
"""支持 `with MysqlDB as session` 便捷获取会话。"""
|
|
62
|
+
self._ctx_session = self.session_factory()
|
|
63
|
+
return self._ctx_session
|
|
64
|
+
|
|
65
|
+
def __exit__(self, exc_type, exc_val, exc_tb):
|
|
66
|
+
session = self._ctx_session
|
|
67
|
+
if session is None:
|
|
68
|
+
return False
|
|
69
|
+
try:
|
|
70
|
+
if exc_type:
|
|
71
|
+
if session.in_transaction():
|
|
72
|
+
session.rollback()
|
|
73
|
+
else:
|
|
74
|
+
if session.in_transaction():
|
|
75
|
+
session.commit()
|
|
76
|
+
finally:
|
|
77
|
+
session.close()
|
|
78
|
+
self._ctx_session = None
|
|
79
|
+
# 返回 False 以便异常向外继续传播
|
|
80
|
+
return False
|
|
81
|
+
|
|
82
|
+
async def __aenter__(self) -> AsyncSession:
|
|
83
|
+
"""支持 `async with MysqlDB as session`。"""
|
|
84
|
+
self._ensure_async_engine()
|
|
85
|
+
self._async_ctx_session = self.async_session_factory()
|
|
86
|
+
return self._async_ctx_session
|
|
87
|
+
|
|
88
|
+
async def __aexit__(self, exc_type, exc_val, exc_tb):
|
|
89
|
+
session = self._async_ctx_session
|
|
90
|
+
if session is None:
|
|
91
|
+
return False
|
|
92
|
+
try:
|
|
93
|
+
if exc_type:
|
|
94
|
+
if session.in_transaction():
|
|
95
|
+
await session.rollback()
|
|
96
|
+
else:
|
|
97
|
+
if session.in_transaction():
|
|
98
|
+
await session.commit()
|
|
99
|
+
finally:
|
|
100
|
+
await session.close()
|
|
101
|
+
self._async_ctx_session = None
|
|
102
|
+
return False
|
|
103
|
+
|
|
57
104
|
def _ensure_async_engine(self):
|
|
58
105
|
"""延迟创建异步引擎(线程安全)"""
|
|
59
106
|
if not self._async_engine_created:
|
|
@@ -4,7 +4,7 @@ with open("README.md", "r", encoding="utf-8") as f:
|
|
|
4
4
|
long_description = f.read()
|
|
5
5
|
setup(
|
|
6
6
|
name="cxppython", # 包名
|
|
7
|
-
version="0.2.
|
|
7
|
+
version="0.2.10", # 版本号
|
|
8
8
|
packages=find_packages(exclude=["tests", "tests.*"]), # 自动找到所有包
|
|
9
9
|
description="A python utils package",
|
|
10
10
|
long_description=long_description,
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|