Twisted, MySQLdb and (2006, 'MySQL server has

2019-04-13 05:29发布

In twisted I am a perpetual event loop that is always lookig for a new query to run It polls a SQS queue and are are times where time between queres is long enough to time out and this is the error I get when a new query arrives...

MySQLdb _mysql_exceptions.OperationalError: (2006, 'MySQL server has gone away')

here is my connection

self.pool = adbapi.ConnectionPool("MySQLdb", self.parms['host'], self.parms['username'], self.parms['password'], self.parms['database'])

Here is the logic I use to try and solve the problem.

try:
    d = self.pool.runQuery(query, ())
except:
    self.pool = adbapi.ConnectionPool("MySQLdb", self.parms['host'], self.parms['username'], self.parms['password'], self.parms['database']) 
    d = self.pool.runQuery(query, ())
    print 'Reconnecting'

Problem is that it does not seem to work very well. So..if a get a 206 error the try reconnection and execute the query again. What is best practice to solve this problem?

Thanks

1条回答
兄弟一词,经得起流年.
2楼-- · 2019-04-13 05:54

I have no experience with adbapi, but cp_reconnect parameter, mentioned in docs, looks promising.

Thus your pool initialization will look something like:

self.pool = adbapi.ConnectionPool("MySQLdb", self.parms['host'], self.parms['username'], self.parms['password'], self.parms['database'], cp_reconnect=True)
查看更多
登录 后发表回答