时间一长,什么样的坑都能出现。
Flask的项目碰到了这样的错误:
pymysql.err.InterfaceError
InterfaceError: (0, '')
Traceback (most recent call last)
-
File "/usr/lib64/python2.7/site-packages/flask/app.py", line 2309, in
__call__
return self.wsgi_app(environ, start_response)
-
File "/usr/lib64/python2.7/site-packages/flask/app.py", line 2295, in
wsgi_app
response = self.handle_exception(e)
-
File "/usr/lib64/python2.7/site-packages/flask/app.py", line 1741, in
handle_exception
reraise(exc_type, exc_value, tb)
-
File "/usr/lib64/python2.7/site-packages/flask/app.py", line 2292, in
wsgi_app
response = self.full_dispatch_request()
-
File "/usr/lib64/python2.7/site-packages/flask/app.py", line 1815, in
full_dispatch_request
rv = self.handle_user_exception(e)
-
File "/usr/lib64/python2.7/site-packages/flask/app.py", line 1718, in
handle_user_exception
reraise(exc_type, exc_value, tb)
-
File "/usr/lib64/python2.7/site-packages/flask/app.py", line 1813, in
full_dispatch_request
rv = self.dispatch_request()
-
File "/usr/lib64/python2.7/site-packages/flask/app.py", line 1799, in
dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/home/user1/exihibition/exihibition.py", line 22, in twitter
cur.execute(sql)
-
File "/usr/lib64/python2.7/site-packages/pymysql/cursors.py", line 170, in
execute
result = self._query(query)
-
File "/usr/lib64/python2.7/site-packages/pymysql/cursors.py", line 328, in
_query
conn.query(q)
-
File "/usr/lib64/python2.7/site-packages/pymysql/connections.py", line 515, in
query
self._execute_command(COMMAND.COM_QUERY, sql)
-
File "/usr/lib64/python2.7/site-packages/pymysql/connections.py", line 745, in
_execute_command
raise err.InterfaceError("(0, '')")
InterfaceError: (0, '')
在网上看到的原因是说MySQL连接超过8小时没有任何操作时,MySQL会自动断开连接。一种推荐的方式是使用db.ping(),即在每次进行查询或者插入相关操作时,先检查数据库的连接是否正常,如果断开了,则重新连接。
db.ping(reconnect=True)
cur.execute(sql)
db.commit()