一.try...except...无法捕获此类异常
背景:多线程执行以下mydb_insert函数,错误发生在 res=cursor.execute(sql, [my_id, url, html_zh, html_en, crawl_time,flag]) 一行,但是异常无法捕获,只能通过如下方式捕获异常:(不知道原因)
try:
mydb_insert()
except:
...
mydb_insert函数如下:
def mydb_insert(self,url,html_zh,html_en,flag):
self.conn.ping(reconnect=True)
cursor = self.conn.cursor()
sql='insert into url_table (my_id,url,html_zh,html_en,time,is_get_success) values (%s,%s,%s,%s,%s,%s);'
crawl_time=datetime.datetime.now()
crawl_time=str(crawl_time).strip().split(' ')[0]
my_id=url.split('/')[4]
try:
res=cursor.execute(sql, [my_id, url, html_zh, html_en, crawl_time,flag])
self.conn.commit()
except:
self.conn.rollback()
cursor.close()
二.报错汇总
1. pymysql.err.OperationalError: (1153, "Got a packet bigger than 'max_allowed_packet' bytes")
Traceback (most recent call last):
File "/home/qinbo/google/code/Mydb.py", line 24, in mydb_insert
res=cursor.execute(sql, [my_id, url, html_zh, html_en, crawl_time,flag])
File "/home/qinbo/.local/lib/python3.6/site-packages/pymysql/cursors.py",