Django 中对 Gevent 友好的数据库连接池
项目描述
感谢 SQLAlchemy,我们可以在使用 Gevent 的猴子补丁时将数据库连接池化。
它支持 MySQL,并且已经在 Python 2.7、Django 1.11、Gevent 1.2 和 SQLAlchemy 1.2 下进行了测试。
此外,DjangoQueuePool是扩展 SQLAlchemy 的QueuePool的新队列池:
重用突发流量溢出的数据库连接;
随着时间的推移逐渐淘汰未使用的数据库连接。
请记住关闭不可用或过时的数据库连接:
关闭的连接返回到池中;
建议在完成任务后关闭它们。
建议尽可能将CONN_MAX_AGE设置为0;
如果CONN_MAX_AGE为0,则连接将始终过时。
from django.db import connections
for conn in connections.all():
conn.close_if_unusable_or_obsolete()
入门
安装数据库连接池
pip install django-db-conn-pool
将池添加到 Django 数据库后端
DATABASES = {
'default': {
'ENGINE': 'django_db_conn_pool.mysqlalchemy',
'CONN_MAX_AGE': 0,
'POOL': db_conn_pool,
...
}
}
选择和调整连接池参数
from sqlalchemy.pool import QueuePool
from django_db_conn_pool.mysqlalchemy.pool import DjangoQueuePool
db_conn_pool = slow_and_safe = {
'django_pool_class': QueuePool, # sqlalchemy's builtin queue pool class
'django_pre_ping': True, # pre ping by django if dialect is None
'django_reset_on_return': False, # use sqlalchemy's reset on conn return
'pool_size': 5, # daily traffic: reuse long connections
'max_overflow': 0, # burst traffic: do not overload the db
'timeout': 30, # burst traffic: > external api timeout
'recycle': 120, # should be smaller than mysql timeout
'dialect': None, # sqlalchemy's mysql dialect instance
'pre_ping': False, # sqlalchemy pre ping requires dialect
'use_threadlocal': True, # every thread always get its same conn
'reset_on_return': 'rollback', # reset on every conn return by rollback
}
db_conn_pool = fast_and_sane = {
'django_pool_class': QueuePool, # sqlalchemy's builtin queue pool class
'django_pre_ping': False, # no pre ping due to long mysql timeout
'django_reset_on_return': True, # reset by rollback only when necessary
'pool_size': 5, # daily traffic: reuse long connections
'max_overflow': 10, # burst traffic: do not overload the db
'timeout': 30, # burst traffic: > external api timeout
'recycle': 3600, # to be much smaller than mysql timeout
'dialect': None, # sqlalchemy's mysql dialect instance
'pre_ping': False, # sqlalchemy pre ping requires dialect
'use_threadlocal': False, # diff threads share the db connections
'reset_on_return': None, # do not use sqlalchemy reset on return
}
db_conn_pool = fast_and_wild = {
'django_pool_class': DjangoQueuePool, # customized from sqlalchemy queue pool
'django_pre_ping': False, # no pre ping due to long mysql timeout
'django_reset_on_return': True, # reset by rollback only when necessary
'django_core_pool_size': 5, # retire no conn if achieving core size
'django_unload_timeout': 2, # wait some random time before overload
'django_retire_interval': 5, # retire few non-core conn per interval
'django_retire_quantity': 1, # retire few non-core conn per interval
'pool_size': 30, # daily traffic: recycle or retire conn
'max_overflow': 0, # burst traffic: put overflow into pool
'timeout': 30, # burst traffic: > external api timeout
'recycle': 3600, # to be much smaller than mysql timeout
'dialect': None, # sqlalchemy's mysql dialect instance
'pre_ping': False, # sqlalchemy pre ping requires dialect
'use_threadlocal': False, # diff threads share the db connections
'reset_on_return': None, # do not use sqlalchemy reset on return
}
发布历史
1.0.0 (2019-05-04)
生日
项目详情
下载文件
下载适用于您平台的文件。如果您不确定要选择哪个,请了解有关安装包的更多信息。
源分布
django-db-conn-pool-1.0.0.tar.gz
(10.6 kB
查看哈希)
内置分布
关
django_db_conn_pool -1.0.0-py3-none-any.whl 的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | 0ddc5233531eb8639f413e150f8e80931d7aad1a99bcd11ad98bc45274bc2df2 |
|
MD5 | db9adbb8e7c7211b55694ae1a4a57155 |
|
布莱克2-256 | 370458a2a6ce9d3410643e1f719cfef60bd93a988009c856b0cd2f5d085aa420 |