使用gevent向django添加db连接池(基于django db gevent pool)
django-geventpool的Python项目详细描述
Django Geventpool
===========
图片::https://img.shields.io/pypi/v/django db geventpool.svg
:目标:https://pypi.python.org/pypi/django geventpool
:alt:pypi version
图片::http://img.shields.io/pypi/l/django db gevent pool.svg
:target:https://github.com/erickponce/django db geventpool
:alt:pypi license
对于django>;=1.6)*
patch psycopg2
----
在使用池之前,如果使用"gunicorn webserver<",psycopg2必须使用psycogreen修补;http://www.gunicorn.org/>;` `,
一个好地方是配置文件中的"post_fork()<;http://docs.gunicorn.org/en/latest/settings.html ` ` post fork>;` `函数:
。代码::python
from psycogreen.gevent import patch_psycopg如果使用来自psycogreen的gevent workers
,请使用此命令。eventlet import patch_psycopg如果使用eventlet workers
def post_fork(服务器,worker):
patch_psycopg()
worker.log.info("made psycopg2 green")
*'django_geventpool.backends.postgis'*
+add*max_conns*to*options*设置允许到数据库的最大连接数(默认值=4)
+如果使用django 1.6或更高版本,则将*'conn_max_age':0*添加到设置以禁用默认django永久连接功能。如果要手动生成greenlets
,请阅读下面的注释。代码::python
#未与sqlite3一起使用。
"密码":"postgres";"未与sqlite3一起使用。
"主机":","设置为本地主机的空字符串。未与sqlite3一起使用。
"端口":",默认设置为空字符串。不与sqlite3一起使用。
"选项":{
"最大连接数":30,"设置允许的最大连接数"
"连接空闲超时":60,"设置为自动关闭空闲连接"
"连接等待超时":15,"设置等待连接时超时
"on_release_discard廑all":false廑设置为true以在释放连接时运行discard廑all
}
}
'默认':{
'引擎':'django-geventpool.backends.postgresql-psycopg2',
'名称':'db',
'用户':'postgres',
'密码':'postgres',
'主机':',
'端口':',
'原子请求':false,
"连接最长时间":0,
"选项":{
"连接最长时间":30,";设置允许的最大连接数"
"连接空闲时间":60,";设置为自动关闭空闲连接"
"连接等待时间":15,#设置等待连接时的超时
"on_release_discard廑all":false设置为在释放连接时运行discard廑all
}
}
django 1.6+与芹菜(或其他),或有代码,手动生成greenlets it不足以将conn_max_age设置为0。
django只在完成请求时检查长时间有效的连接-因此,如果手动生成一个greenlet(或有一个任务生成一个greenlet),它的连接将
不会被清理,并且将一直有效到超时。在生产中,这可能会导致相当多的连接打开,而在开发时,这可能会妨碍您的测试用例。
若要解决此问题,请确保每个greenlet在结束之前发送django.core.signals.request_finished信号或调用django.db.close_old_connections()。装饰方法是首选方法,但其他方法也有效。
…代码::python
from django_db_geventpool.utils import close_connection
@close_connection
def greenlet_worker()
…
代码::来自django.core.signals的python
代码::来自django.db的python
`
*`Django PostgreSQL<;https://github.com/kennethreitz/Django Postgrespool>;。`_
===========
图片::https://img.shields.io/pypi/v/django db geventpool.svg
:目标:https://pypi.python.org/pypi/django geventpool
:alt:pypi version
图片::http://img.shields.io/pypi/l/django db gevent pool.svg
:target:https://github.com/erickponce/django db geventpool
:alt:pypi license
对于django>;=1.6)*
patch psycopg2
----
在使用池之前,如果使用"gunicorn webserver<",psycopg2必须使用psycogreen修补;http://www.gunicorn.org/>;` `,
一个好地方是配置文件中的"post_fork()<;http://docs.gunicorn.org/en/latest/settings.html ` ` post fork>;` `函数:
。代码::python
from psycogreen.gevent import patch_psycopg如果使用来自psycogreen的gevent workers
,请使用此命令。eventlet import patch_psycopg如果使用eventlet workers
def post_fork(服务器,worker):
patch_psycopg()
worker.log.info("made psycopg2 green")
*'django_geventpool.backends.postgis'*
+add*max_conns*to*options*设置允许到数据库的最大连接数(默认值=4)
+如果使用django 1.6或更高版本,则将*'conn_max_age':0*添加到设置以禁用默认django永久连接功能。如果要手动生成greenlets
,请阅读下面的注释。代码::python
#未与sqlite3一起使用。
"密码":"postgres";"未与sqlite3一起使用。
"主机":","设置为本地主机的空字符串。未与sqlite3一起使用。
"端口":",默认设置为空字符串。不与sqlite3一起使用。
"选项":{
"最大连接数":30,"设置允许的最大连接数"
"连接空闲超时":60,"设置为自动关闭空闲连接"
"连接等待超时":15,"设置等待连接时超时
"on_release_discard廑all":false廑设置为true以在释放连接时运行discard廑all
}
}
'默认':{
'引擎':'django-geventpool.backends.postgresql-psycopg2',
'名称':'db',
'用户':'postgres',
'密码':'postgres',
'主机':',
'端口':',
'原子请求':false,
"连接最长时间":0,
"选项":{
"连接最长时间":30,";设置允许的最大连接数"
"连接空闲时间":60,";设置为自动关闭空闲连接"
"连接等待时间":15,#设置等待连接时的超时
"on_release_discard廑all":false设置为在释放连接时运行discard廑all
}
}
django 1.6+与芹菜(或其他),或有代码,手动生成greenlets it不足以将conn_max_age设置为0。
django只在完成请求时检查长时间有效的连接-因此,如果手动生成一个greenlet(或有一个任务生成一个greenlet),它的连接将
不会被清理,并且将一直有效到超时。在生产中,这可能会导致相当多的连接打开,而在开发时,这可能会妨碍您的测试用例。
若要解决此问题,请确保每个greenlet在结束之前发送django.core.signals.request_finished信号或调用django.db.close_old_connections()。装饰方法是首选方法,但其他方法也有效。
…代码::python
from django_db_geventpool.utils import close_connection
@close_connection
def greenlet_worker()
…
代码::来自django.core.signals的python
代码::来自django.db的python
`
*`Django PostgreSQL<;https://github.com/kennethreitz/Django Postgrespool>;。`_