python报错处理_python mysql 断连报错处理
在使用python 對wordpress tag 進行細化代碼處理時,遇到了調用MySQLdb模塊時的出錯,由于錯誤提示和問題原因相差甚遠,查看了N久代碼也未發現代碼有問題。后來問了下師傅,被告知MySQLdb里有一個斷接的坑 ,需要進行數據庫重連解決。
一、報錯代碼及提示
運行出錯的代碼如下:
import MySQLdb
def getTerm(db,tag):
cursor = db.cursor()
query = "SELECT term_id FROM wp_terms where name=%s "
count = cursor.execute(query,tag)
rows = cursor.fetchall()
db.commit()
#db.close()
if count:
term_id = [int(rows[id][0]) for id in range(count)]
return term_id
else:return None
def addTerm(db,tag):
cursor = db.cursor()
query = "INSERT into wp_terms (name,slug,term_group) values (%s,%s,0)"
data = (tag,tag)
cursor.execute(query,data)
db.commit()
term_id = cursor.lastrowid
sql = "INSERT into wp_term_taxonomy (term_id,taxonomy,description) values (%s,'post_tag',%s) "
value = (term_id,tag)
cursor.execute(sql,value)
db.commit()
db.close()
return int(term_id)
dbconn = MySQLdb.connect(host='localhost', user='root', passwd='123456', db='361way', port=3306, charset='utf8', init_command='set names utf8')
tags = ['mysql','1111','aaaa','bbbb','ccccc','php','abc','python','java']
tagids = []
for tag in tags:
termid = getTerm(dbconn,tag)
if termid:
print tag, 'tag id is ',termid
tagids.extend(termid)
else:
termid = addTerm(dbconn,tag)
print 'add tag',tag,'id is ' ,termid
tagids.append(termid)
print 'tag id is ',tagids
直接可以執行,在第for循環里第二次調用getTerm函數時,報錯如下:
Traceback (most recent call last):
File "a.py", line 40, in
termid = getTerm(dbconn,tag)
File "a.py", line 11, in getTerm
count = cursor.execute(query,tag)
File "/usr/lib64/python2.6/site-packages/MySQLdb/cursors.py", line 154, in execute
charset = db.character_set_name()
_mysql_exceptions.InterfaceError: (0, '')
二、解決方法
初始時以為是編碼問題了,又細核對了幾遍未發現編碼有問題,在python代碼里也未發現異常。后來問過師傅后,師傅來了句提示:
只看代碼有啥用,mysql 的超時時間調長點或捕獲異常從連,原因是
cursor. connection 沒有關閉
但是socket已經斷了
cursor 這個行為不會再建立一次socket的
重新執行一次MysqlDB.connect()
看的有點懵懂,先從mysql 里查看了所有timeout相關的變量
mysql> show GLOBAL VARIABLES like "%timeout%";
+----------------------------+-------+
| Variable_name | Value |
+----------------------------+-------+
| connect_timeout | 10 |
| delayed_insert_timeout | 300 |
| innodb_lock_wait_timeout | 50 |
| innodb_rollback_on_timeout | OFF |
| interactive_timeout | 28800 |
| net_read_timeout | 30 |
| net_write_timeout | 60 |
| slave_net_timeout | 3600 |
| table_lock_wait_timeout | 50 |
| wait_timeout | 28800 |
+----------------------------+-------+
10 rows in set (0.00 sec)
發現最小的超時時間是10s ,而我的程序執行起來顯然就不了10s 。因為之前查過相關的報錯,這里估計這個很可能是另外一個報錯:2006,MySQL server has gone away ?。即然和這個超時時間應該沒關系,那就嘗試通過MySQLdb ping測試,如果捕獲異常,就再進行重連,修改后的代碼為:
#!/usr/bin/python
#coding=utf-8
import MySQLdb
def getTerm(db,tag):
cursor = db.cursor()
query = "SELECT term_id FROM wp_terms where name=%s "
count = cursor.execute(query,tag)
rows = cursor.fetchall()
db.commit()
#db.close()
if count:
term_id = [int(rows[id][0]) for id in range(count)]
print term_id
return term_id
else:return None
def addTerm(db,tag):
cursor = db.cursor()
query = "INSERT into wp_terms (name,slug,term_group) values (%s,%s,0)"
data = (tag,tag)
cursor.execute(query,data)
db.commit()
term_id = cursor.lastrowid
sql = "INSERT into wp_term_taxonomy (term_id,taxonomy,description) values (%s,'post_tag',%s) "
value = (term_id,tag)
cursor.execute(sql,value)
db.commit()
db.close()
return int(term_id)
dbconn = MySQLdb.connect(host='localhost', user='root', passwd='123456', db='361way', port=3306, charset='utf8', init_command='set names utf8')
tags = ['mysql','1111','aaaa','bbbb','ccccc','php','abc','python','java']
if __name__ == "__main__":
tagids = []
for tag in tags:
try:
dbconn.ping()
except:
print 'mysql connect have been close'
dbconn = MySQLdb.connect(host='localhost', user='root', passwd='123456', db='361way', port=3306, charset='utf8', init_command='set names utf8')
termid = getTerm(dbconn,tag)
if termid:
print tag, 'tag id is ',termid
tagids.extend(termid)
else:
termid = addTerm(dbconn,tag)
print 'add tag',tag,'id is ' ,termid
tagids.append(termid)
print 'All tags id is ',tagids
再執行發現竟然OK了,而細看下結果,發現基本上每1-2次getTerm或addTerm函數調用就會打印一次'mysql connect have been close' 。這里問題雖然已經解決,不過并未細挖到真正的根因。
三、建議
從網上要看的結果來看MySQLdb還有不支持長連接的坑,這個可以通過上面提到的修改my.cnf參數解決,也可以考慮使用其他支持設置timeout時間mysql模塊。另外,也建議使用MySQLdb時,可以考慮使用一個對其二次封裝的模塊torndb ----?A lightweight wrapper around MySQLdb 。
總結
以上是生活随笔為你收集整理的python报错处理_python mysql 断连报错处理的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python select模块_Pyth
- 下一篇: gorm 返回多条数据_如何优雅的操作数