Twisted, MySQLdb and (2006, 'MySQL server has gone away') using Twisted adbapi

☆樱花仙子☆ 提交于 2019-12-09 20:48:01

问题


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:


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)


来源:https://stackoverflow.com/questions/7656499/twisted-mysqldb-and-2006-mysql-server-has-gone-away-using-twisted-adbapi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!