之前因为线程内出现异常导致Python部分线程退出,程序以单线程方式运行了好几个星期,浪费了很多时间。
如何查看当前进程有多少个存活线程呢?
方法1)pstree -p 4761(进程ID)。如下截图所示:

方法2)cat /proc/4761(进程ID)/status。如下图所示:

方法3)top -p 4761(进程ID),然后按H。如下图所示:

为什么只有1个线程在running呢? 由于
GIL的缘故,Python同时只能使用一个CPU核心,线程间要轮换执行,所以同时最多有一个线程在运行。
看下图,MySQL的多个线程就可以同时处于running状态,因为C++可以使用多个CPU核心。