初始化项目
This commit is contained in:
@@ -0,0 +1,54 @@
|
||||
import datetime
|
||||
|
||||
from sqlalchemy.event import listen
|
||||
from sqlalchemy.pool import Pool, QueuePool
|
||||
|
||||
from paste.db import engine
|
||||
|
||||
AsyncPoolMaxCheckOut = 0
|
||||
"""
|
||||
异步连接池最大连接数量。
|
||||
"""
|
||||
|
||||
AsyncPoolMaxCheckOutAt = datetime.datetime.now()
|
||||
"""
|
||||
异步连接池最大连接数量发生时间。
|
||||
"""
|
||||
|
||||
GlobalPoolMaxCheckOut = 0
|
||||
"""
|
||||
普通连接池最大连接数量。
|
||||
"""
|
||||
|
||||
GlobalPoolMaxCheckOutAt = datetime.datetime.now()
|
||||
"""
|
||||
普通连接池最大连接数量发生时间。
|
||||
"""
|
||||
|
||||
|
||||
def on_checkout(dbapi_connection, connection_record, connection_proxy):
|
||||
"""
|
||||
当取用连接池中的连接后,立即执行的事件。这里用来记录取用峰值数据。
|
||||
"""
|
||||
async_pool: QueuePool = engine.async_connect_engine().pool
|
||||
global_pool = engine.connect_engine().pool
|
||||
assert isinstance(global_pool, QueuePool), f"引擎类型错误."
|
||||
|
||||
global AsyncPoolMaxCheckOut
|
||||
global AsyncPoolMaxCheckOutAt
|
||||
global GlobalPoolMaxCheckOut
|
||||
global GlobalPoolMaxCheckOutAt
|
||||
|
||||
_async_checkout = async_pool.checkedout()
|
||||
if AsyncPoolMaxCheckOut < _async_checkout:
|
||||
AsyncPoolMaxCheckOut = _async_checkout
|
||||
AsyncPoolMaxCheckOutAt = datetime.datetime.now()
|
||||
|
||||
_global_checkout = global_pool.checkedout()
|
||||
if GlobalPoolMaxCheckOut < _global_checkout:
|
||||
GlobalPoolMaxCheckOut = _global_checkout
|
||||
GlobalPoolMaxCheckOutAt = datetime.datetime.now()
|
||||
|
||||
|
||||
def bind_listener():
|
||||
listen(Pool, 'checkout', on_checkout)
|
||||
Reference in New Issue
Block a user