最近接手的是個python腳本,經過crontab按期運行,用facebook提供的api抓取數據,處理後再插入到數據庫.css
許多人反映這個服務不穩定,抓取的數據不全.html
我在測試的過程當中發現,這個腳本開發的時間已通過了好久了,facebook提供的接口數據已經有所調整. 因此會有數據python
格式不對的問題;還有就是在跑了個小時後,會拋出 ‘MySQL server has gone away’ 的異常..mysql
網上找到的相似案例以下:git
http://stackoverflow.com/questions/8689649/solving-mysql-server-has-gone-away-errorsgithub
http://stackoverflow.com/questions/7942154/mysql-error-2006-mysql-server-has-gone-awaysql
我觀察到操做數據庫的類庫是torndb,已通過去這麼久,會不會這個bug在官方也有反應並修復了呢.數據庫
果不其然..api
1:
2: def _ensure_connected(self):
3: # Mysql by default closes client connections that are idle for
4: # 8 hours, but the client library does not report this fact until
5: # you try to perform a query and it fails. Protect against this
6: # case by preemptively closing and reopening the connection
7: # if it has been idle for too long (7 hours by default).
8: if (self._db is None or
9: (time.time() - self._last_use_time > self.max_idle_time)):
10: self.reconnect()
11: self._last_use_time = time.time()