Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Peewee MySQL server has gone away

I use flask and peewee. Sometimes peewee throws this error

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

Peewee database connection

db = PooledMySQLDatabase(database,**{
            "passwd": password, "user": user,
            "max_connections":None,"stale_timeout":None,
            "threadlocals" : True
        })

@app.before_request
def before_request():
    db.connect()

@app.teardown_request
def teardown_request(exception):
    db.close()

After mysql error that "MySQL server has gone away (error(32, 'Broken pipe'))", select queries works without problem, but insert,update,delete queries don't work.

On insert,update,delete queries works behind(in mysql) but peewee throw this errors.

(2006, "MySQL server has gone away (error(32, 'Broken pipe'))")
like image 724
Alexander Avatar asked Dec 02 '15 08:12

Alexander


People also ask

How do I use peewee in Python?

When starting a project with peewee, it's typically best to begin with your data model, by defining one or more Model classes: from peewee import * db = SqliteDatabase('people. db') class Person(Model): name = CharField() birthday = DateField() class Meta: database = db # This model uses the "people.


1 Answers

The peewee documentation has talked about this problem, here is the link: Error 2006: MySQL server has gone away

This particular error can occur when MySQL kills an idle database connection. This typically happens with web apps that do not explicitly manage database connections. What happens is your application starts, a connection is opened to handle the first query that executes, and, since that connection is never closed, it remains open, waiting for more queries.

So you have some problems on managing your database connection.


Since I can't reproduce your problem, could you please try this one, close your database this way:

@app.teardown_appcontext
def close_database(error):
    db.close()

And you may get some info from the doc: Step 3: Database Connections

like image 183
lord63. j Avatar answered Sep 20 '22 22:09

lord63. j