Python celery Worker exited prematurely: signal 11 (SIGSEGV) --一种解决方案

Python libmysqlclient segfault (MySQLdb)

Python多线程共享Mysql连接出错?

python mysqldb多线程问题

python自制简易mysql连接池的实现示例

MySQLdb、Mysqlclient、PyMySQL 三个python的MySQL库的比较和总结

问题

线上环境发现error

billiard.exceptions.WorkerLostError: Worker exited prematurely: signal 11 (SIGSEGV).
Process 'Worker-47' pid:25883 exited with 'signal 11 (SIGSEGV)'

resolve_monitor_alarms这个celery任务在worker执行时出现未知异常,并导致worker进程结束。

错误是signal 11 (SIGSEGV),这是一个linux signal信号,signal 11标识Segmentation violation,即段异常,涉及到linux内核的数据异常。

排查

这是一个系统内核的错误,那我们首先去看服务器的系统日志。

/var/log/messages

因为我们的任务是每小时的整点执行一次,缩小范围直接看每小时0分的日志。

经过简单的分析,发现每小时都会有这样一条错误日志

kernel: python[6406]: segfault at 1b8 ip 00007f24ea928fc8 sp 00007f24d3ffdc20 error 4 in libmysqlclient.so.18.0.0[7f24ea8f7000+2de000]

某个python进程在使用libmysqlclient库的使用遇到segfault段错误

那么问题就定位到mysql了,看下这个任务的具体代码

@classmethod
def alarm_resolved(cls, alarm_source_obj):
"""蓝监监控告警恢复""" def _update_bk_monitor_alarms_status_thread(_alarm_list, _semaphore, _cursor):
# 线程量增加
_semaphore.acquire()
try:
# ...
sql = "xxx"
logger.debug(sql)
_cursor.execute(sql)
rows = _cursor.fetchall() # ... except Exception as error:
logger.exception("_update_bk_monitor_alarms_status_thread error, detail info: " "[error: %s]" % error) finally:
# 线程量减少
_semaphore.release() # ...
conn = MySQLdb.connect(host=host, user=user, password=password, database=database, charset="utf8")
cursor = conn.cursor()
# ... # 分批去数据库获取告警事件状态, 分批的原因是mysql query查询大小有限制
once_query_num = 500
semaphore = threading.BoundedSemaphore(4)
threads = []
for alarm_list in [
active_alarms[i : i + once_query_num] for i in range(0, len(active_alarms), once_query_num) # noqa
]:
t = threading.Thread(target=_update_bk_monitor_alarms_status_thread, args=(alarm_list, semaphore, cursor))
threads.append(t)
t.start() for t in threads:
t.join() cursor.close()
conn.close()

使用的MySQLdb模块,打开一个数据库连接,然后把游标共享给多个线程去查询数据库。

这里其实是mysqlclient模块,MySQLdb的fork版本用来兼容python3

我试着给线程的开始和结束打了log,分析发现有一部分线程执行失败了没有结束log。

所以应该是某个子线程拿着游标去查询数据库时引发了内核segfault。

在StackOverflow上找到了如下解决办法:



为子线程开启自己的mysql连接

经过测试,确实解决了问题。

分析

很明显MySQLdb打开的连接不能被多线程共享,可能会引发内核问题。

看一下官方文档的解释:

threadsafety
Integer constant stating the level of thread safety the interface supports. This is set to 1, which means: Threads may share the module. The MySQL protocol can not handle multiple threads using the same connection at once. Some earlier versions of MySQLdb utilized locking to achieve a threadsafety of 2. While this is not terribly hard to accomplish using the standard Cursor class (which uses mysql_store_result()), it is complicated by SSCursor (which uses mysql_use_result(); with the latter you must ensure all the rows have been read before another query can be executed. It is further complicated by the addition of transactions, since transactions start when a cursor execute a query, but end when COMMIT or ROLLBACK is executed by the Connection object. Two threads simply cannot share a connection while a transaction is in progress, in addition to not being able to share it during query execution. This excessively complicated the code to the point where it just isn't worth it. The general upshot of this is: Don't share connections between threads. It's really not worth your effort or mine, and in the end, will probably hurt performance, since the MySQL server runs a separate thread for each connection. You can certainly do things like cache connections in a pool, and give those connections to one thread at a time. If you let two threads use a connection simultaneously, the MySQL client library will probably upchuck and die. You have been warned. For threaded applications, try using a connection pool. This can be done using the Pool module.

有一个叫threadsafety的参数,控制线程安全的级别。

它被设置为1,意味着多线程只能共享模块。

  • 0 多线程不能共享模块
  • 1 多线程可以共享模块
  • 2 多线程可以共享模块和连接
  • 3 多线程可以共享模块、连接和游标

但是看了一下这只是一个声明,MySQLdb只支持threadsafety=1,也就是多线程只能共享模块,不能共享连接和游标。原因大概是2和3的实现较为昂贵和复杂。

MySQLdb不建议我们多线程共享连接,不值得花费精力去实现,并且可能带来性能问题,因为mysql server为每一个连接都会运行一个单独的线程。

MySQLdb建议我们使用连接池去缓存多个连接,缓存池初始化后,每个线程都从缓存池中去获取自己的连接,并在线程结束归还到连接池。

MySQLdb还指明,如果多个线程同时使用了一个mysql连接,我们的mysql client很可能会崩溃死掉。

解决办法

所以对我们的问题,有两个解决办法:

  • 自己维护一个数据库连接池为每个线程分配单独的连接
  • 直接在每个线程中打开新的连接

前者性能好,后者代码实现快。

总结

MySQLdb连接线程不安全导致的问题,需要为线程分配单独的数据库连接。

django.db是用连接池实现的,不会有问题。

但我们直接使用一些mysql模块的时候要注意线程安全问题。

Python MySQLdb连接被多线程共享引发的内核segfault段错误的更多相关文章

  1. python MySQLdb连接mysql失败(转载)

    最近了解了一下django,数据库选用了mysql, 在连接数据库的过程中,遇到一点小问题,在这里记录一下,希望能够对遇到同样的问题的朋友有所帮助,少走一些弯路.关于django,想在这里也额外说一句 ...

  2. 7.Python网络编程_多线程共享全局变量问题

    Python多线程支持全局变量的共享操作,但是它存在很多问题,先来看以下程序,该程序理论上执行完毕后全局变量g_num的值应该是2000000,但是在实际运行中,结果不足理论值 import thre ...

  3. python -- MySQLdb连接mysql数据库

    1. python安装mysql $ pip install mysql-python 2. 数据库连接程序: import MySQLdb # 打开数据库连接db = MySQLdb.connect ...

  4. python MySQLdb连接mysql时报错

    故障现象: >>> import MySQLdb >>> conn = MySQLdb.connect(host=,charset="utf8" ...

  5. 一起学Python: 多线程-共享全局变量问题

    多线程-共享全局变量问题 多线程开发可能遇到的问题 假设两个线程t1和t2都要对全局变量g_num(默认是0)进行加1运算,t1和t2都各对g_num加10次,g_num的最终的结果应该为20. 但是 ...

  6. 一起学Python:多线程-共享全局变量

    多线程-共享全局变量 from threading import Thread import time g_num = 100 def work1(): global g_num for i in r ...

  7. Python 中多线程共享全局变量的问题

    写在前面不得不看的一些P话: Python 中多个线程之间是可以共享全局变量的数据的. 但是,多线程共享全局变量是会出问题的. 假设两个线程 t1 和 t2 都要对全局变量g_num (默认是0)进行 ...

  8. python多线程-共享全局变量

    目录 多线程-共享全局变量 多线程-共享全局变量 列表当作实参传递到线程中 总结 多线程-共享全局变量问题 多线程开发可能遇到的问题 测试1 测试2 多线程-共享全局变量 多线程-共享全局变量 imp ...

  9. Python MySQLdb模块连接操作mysql数据库实例_python

    mysql是一个优秀的开源数据库,它现在的应用非常的广泛,因此很有必要简单的介绍一下用python操作mysql数据库的方法.python操作数据库需要安装一个第三方的模块,在http://mysql ...

  10. python编程系列---多线程共享全局变量出现了安全问题的解决方法

    多线程共享全局变量出现了安全问题的解决方法 当多线程共享全局变量时,可能出现安全问题,解决机制----互斥锁:即在在一段与全局变量修改相关的代码中,假设一个时间片不足以完成全局变量的修改,就在这段代码 ...

随机推荐

  1. JS实现打字效果(_会闪烁)

    背景 更新博客园个人博客时,突发奇想想要将子标题的入场特效做成类似Linux命令行输命令时的样式 效果网页:LanceEst - 博客园 (cnblogs.com) 思路和代码 1 <h2 id ...

  2. oracle WMSYS.WM_CONCAT 函数使用

    1.用法 WMSYS.WM_CONCAT(要连接的字段) 该函数返回来自同一个分组的指定字段的非NULL值的连接起来字符串,默认逗号连接,一般搭配分组函数使用 2.示例 select XFJ_ID,R ...

  3. M1处理器的电脑xcode模拟器编译报错问题详解及解决方案

    在M1芯片的苹果电脑中使用Xcode编译模拟器时,可能会碰到如下报错: 原因是由于M1模拟器架构是arm64架构,而Intel芯片是x86_64的架构,从而导致编译出现了问题. 这些报错,都是是由于项 ...

  4. HTML学习笔记3----制作一个简易网站

    随笔记录方便自己和同路人查阅. #------------------------------------------------我是可耻的分割线--------------------------- ...

  5. PyMySQL更新

    title: PyMySQL更新 author: 杨晓东 permalink: PyMySQL更新 date: 2021-10-02 11:27:04 categories: - 投篮 tags: - ...

  6. WebService 客户端上传图片,服务器端接收图片并保存到本地

    需求:如题,C#本地要调用Webservice接口,上传本地的照片到服务器中: 参考:客户端: https://blog.csdn.net/tiegenZ/article/details/799276 ...

  7. 宽字符集(unicode)操作函数 (转)

    字符分类: 宽字符函数 普通C函数 描述 iswalnum() isalnum() 测试字符是否为数字或字母 iswalpha() isalpha() 测试字符是否是字母 iswcntrl() isc ...

  8. 微信字体大小调整导致的H5页面错乱问题处理

    当用户调整微信字体大小时会导致H5页面错乱,解决方案如下: ios:在css中加入-webkit-text-size-adjust: 100% !important;   body {   -webk ...

  9. STM32F103 的 USART5使用

    STM32F103zet6 的串口5是UART5   不是  USART5

  10. 前端-对js原型继承的简单举例

    function A(name,color){ this.name=name; this.color=color;   } A.prototype.getColor=function(){ retur ...