一.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()

本文主要讨论了在使用Pymysql时遇到的一些无法通过try...except...捕获的异常,包括 pymysql.err.OperationalError: (1153, "Got a packet bigger than 'max_allowed_packet' bytes")、pymysql.err.InterfaceError: (0, '') 和 pymysql.err.OperationalError: (2006, "MySQL server has gone away (BrokenPipeError(32, 'Broken pipe'))")。针对这些问题,文章提供了详细的错误原因分析和解决方案,如调整MySQL的max_allowed_packet参数、检查并重连数据库等。"
123718466,5844358,Kubernetes深入解析:从容器到集群管理,"['容器技术', 'Kubernetes架构', 'Docker镜像', '资源隔离', 'Borg项目', '容器编排']
最低0.47元/天 解锁文章
595

被折叠的 条评论
为什么被折叠?



