django为什么不原生支持连接池
连接池的好处这么多,django为什么不原生支持呢?
个人解读就是:
因为python是在GIL 环境下运行,连接池要是保存在thread中,在uwsgi或gunicorn fork运行,连接池的信息就没法共享了,这样反而加大数据库开销。
大家都知道,python的解释器是多种的,常见的cpython, pypy,jpython, 这是都是要做适配的。
最后一点,是业务业景不同,不是所有人都需要使用连接池
以上是浅见。可以附上你们的高见?
加入连接池的姿势
改源码方式
通过源码可以看出,load_backend来加载连接的,可以修改源加入自已写的连接池。
已经有过这样的案例https://www.jianshu.com/p/e210e6380ade
https://github.com/django/django/blob/master/django/db/utils.py
def load_backend(backend_name):
"""
Return a database backend's "base" module given a fully qualified database
backend name, or raise an error if it doesn't exist.
"""
# This backend was renamed in Django 1.9.
if backend_name == 'django.db.backends.postgresql_psycopg2':
backend_name = 'django.db.backends.postgresql'
try:
return import_module('%s.base' % backend_name)
except ImportError as e_user:
# The database backend wasn't found. Display a helpful error message
# listing all possible (built-in) database backends.
backend_dir = os.path.join(os.path.dirname(upath(__file__)), 'backends')
try:
builtin_backends = [
name for _, name, ispkg in pkgutil.iter_modules([npath(backend_dir)])
if ispkg and name not in {'base', 'dummy', 'postgresql_psycopg2'}
]
except EnvironmentError:
builtin_backends = []
if backend_name not in ['django.db.backends.%s' % b for b in
builtin_backends]:
backend_reprs = map(repr, sorted(builtin_backends))
error_msg = ("%r isn't an available database backend.\n"
"Try using 'django.db.backends.XXX', where XXX "
"is one of:\n %s\nError was: %s" %
(backend_name, ", ".join(backend_reprs), e_user))
raise ImproperlyConfigured(error_msg)
else:
# If there's some other error, this must be an error in Django
raise
以应用加载的方式
# settings.py
INSTALLED_APPS = (
'djorm_pool',
...
)
DJORM_POOL_OPTIONS = {
"pool_size": 20,
"max_overflow": 0,
"recycle": 3600, # the default value
}
网友评论