Mysql 不再使用 peewee 作为 Django 中的第二个数据库
Mysql gone away with peewee as second database in Django
我正在使用 peewee
访问远程 MySql 数据库来检索我需要在 Django 中显示的数据 应用程序。这意味着 peewee
并不是真正用于访问主数据库,而只是喜欢定义自定义模型:
databases.py
中的示例:
from django.conf import settings
from playhouse.pool import PooledMySQLDatabase
database = PooledMySQLDatabase(
settings.DB_PEEWEE,
**{
"use_unicode": True,
"charset": "utf8",
"max_connections": 32,
"stale_timeout": 300, # 5 minutes.
"password": settings.DB_PEEWEE_PSWRD,
"user": settings.DB_PEEWEE_USER,
"host": settings.DB_PEEWEE_HOST,
"port": settings.DB_PEEWEE_PORT,
}
)
在 models.py
中:
from .databases import database
class BaseModel(peewee.Model):
class Meta:
database = database
class CountryGroups(BaseModel):
africa = peewee.CharField(null=True)
group_code = peewee.AutoField()
group_name = peewee.CharField()
latin_eur = peewee.CharField(null=True)
type = peewee.CharField()
class Meta:
table_name = "country_groups"
...
# other main django models
因此可以从 views.py
文件中轻松调用模型,如下所示:
CountryGroups_list = (
CountryGroups.select()
.where(CountryGroups.group_name << ["ERROR", "INFO"])
.order_by(CountryGroups.group_name.desc())
.limit(1000)
)
我可以运行查询正常。但是 24 小时后连接断开时出现错误:
(2006, "MySQL server has gone away (error(32, 'Broken pipe'))")
在 Django
中解决此问题的建议方法是使用 middleware 但这假设在这种情况下 peewee
相关的数据库是主要的,并导致在这样的错误中:
File "/home/user/Dev/project/project/wsgi.py", line 14, in <module>
from configurations.wsgi import get_wsgi_application
File "/home/user/.virtualenvs/project/lib/python3.5/site-packages/configurations/wsgi.py", line 14, in <module>
application = get_wsgi_application()
File "/home/user/.virtualenvs/project/lib/python3.5/site-packages/django/core/wsgi.py", line 13, in get_wsgi_application
return WSGIHandler()
File "/home/user/.virtualenvs/project/lib/python3.5/site-packages/django/core/handlers/wsgi.py", line 135, in __init__
self.load_middleware()
File "/home/user/.virtualenvs/project/lib/python3.5/site-packages/django/core/handlers/base.py", line 37, in load_middleware
mw_instance = middleware(handler)
TypeError: object() takes no parameters
所以我的问题是,如何将自动 connect() 和 close() 实现到我的通用 database 模型,这样我就不会得到错误?
您需要编写一个新的 Django 风格的中间件。我已经相应地更新了文档:
http://docs.peewee-orm.com/en/latest/peewee/database.html#django
def PeeweeConnectionMiddleware(get_response):
def middleware(request):
database.connect()
try:
response = get_response(request)
finally:
if not database.is_closed():
database.close()
return response
return middleware
我正在使用 peewee
访问远程 MySql 数据库来检索我需要在 Django 中显示的数据 应用程序。这意味着 peewee
并不是真正用于访问主数据库,而只是喜欢定义自定义模型:
databases.py
中的示例:
from django.conf import settings
from playhouse.pool import PooledMySQLDatabase
database = PooledMySQLDatabase(
settings.DB_PEEWEE,
**{
"use_unicode": True,
"charset": "utf8",
"max_connections": 32,
"stale_timeout": 300, # 5 minutes.
"password": settings.DB_PEEWEE_PSWRD,
"user": settings.DB_PEEWEE_USER,
"host": settings.DB_PEEWEE_HOST,
"port": settings.DB_PEEWEE_PORT,
}
)
在 models.py
中:
from .databases import database
class BaseModel(peewee.Model):
class Meta:
database = database
class CountryGroups(BaseModel):
africa = peewee.CharField(null=True)
group_code = peewee.AutoField()
group_name = peewee.CharField()
latin_eur = peewee.CharField(null=True)
type = peewee.CharField()
class Meta:
table_name = "country_groups"
...
# other main django models
因此可以从 views.py
文件中轻松调用模型,如下所示:
CountryGroups_list = (
CountryGroups.select()
.where(CountryGroups.group_name << ["ERROR", "INFO"])
.order_by(CountryGroups.group_name.desc())
.limit(1000)
)
我可以运行查询正常。但是 24 小时后连接断开时出现错误:
(2006, "MySQL server has gone away (error(32, 'Broken pipe'))")
在 Django
中解决此问题的建议方法是使用 middleware 但这假设在这种情况下 peewee
相关的数据库是主要的,并导致在这样的错误中:
File "/home/user/Dev/project/project/wsgi.py", line 14, in <module>
from configurations.wsgi import get_wsgi_application
File "/home/user/.virtualenvs/project/lib/python3.5/site-packages/configurations/wsgi.py", line 14, in <module>
application = get_wsgi_application()
File "/home/user/.virtualenvs/project/lib/python3.5/site-packages/django/core/wsgi.py", line 13, in get_wsgi_application
return WSGIHandler()
File "/home/user/.virtualenvs/project/lib/python3.5/site-packages/django/core/handlers/wsgi.py", line 135, in __init__
self.load_middleware()
File "/home/user/.virtualenvs/project/lib/python3.5/site-packages/django/core/handlers/base.py", line 37, in load_middleware
mw_instance = middleware(handler)
TypeError: object() takes no parameters
所以我的问题是,如何将自动 connect() 和 close() 实现到我的通用 database 模型,这样我就不会得到错误?
您需要编写一个新的 Django 风格的中间件。我已经相应地更新了文档:
http://docs.peewee-orm.com/en/latest/peewee/database.html#django
def PeeweeConnectionMiddleware(get_response):
def middleware(request):
database.connect()
try:
response = get_response(request)
finally:
if not database.is_closed():
database.close()
return response
return middleware