iMitwe
iMitwe

Reputation: 1258

Mysql gone away with peewee as second database in Django

I'm using peewee to access a remote MySql database to retrieve data that I need to display in a Django app. It means that peewee isn't really used to access the main db but just to like, define custom models:

Example in 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,
    }
)

in 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

So the model can be easily called from the views.py file as :

CountryGroups_list = (
        CountryGroups.select()
        .where(CountryGroups.group_name << ["ERROR", "INFO"])
        .order_by(CountryGroups.group_name.desc())
        .limit(1000)
    )

I can run the query fine. But I get an error after 24 hours where the connection is broken:

(2006, "MySQL server has gone away (error(32, 'Broken pipe'))")

The suggested method of solving this in Django is trough the usage of a middleware but this assume that in that case peewee related db is the main one, and has resulted in errors like this one:

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

So my question is, how would I implement the auto connect() and close() to my generic database model so that I don't get the error?

Upvotes: 0

Views: 302

Answers (1)

coleifer
coleifer

Reputation: 26235

You need to write a new Django-style middleware. i've updated the docs accordingly:

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

Upvotes: 1

Related Questions