该问题的解决主要参考了网上的几篇文章,在此一并谢过。

1、python下使用ctypes获取threading线程id

python的多线程坑坑不断… …

python的threading因为封装的太好, 很多本源的东西在threading对象里是拿不到的.  首先需要说明的是python threading的name跟ident,这些看起来是线程名字,线程id其实只是个标识,注意是标识而已.  简单过了下threading创建对象及启动线程的代码,发现ident跟pstree查到的线程id是两码事.

该文章写的有些乱,欢迎来喷 ! 另外文章后续不断更新中,请到原文地址查看更新http://xiaorui.cc/?p=3017

我在 stackoverflow 查询到了一些关于pyhton线程id的获取方式,但大多数人其实对线程id是不关心的,他们会利用threading给予的threading.currentThread().ident threading.currentThread().name来识别线程.  最后在查到一老外写的使用ctypes调用系统的动态链接库libc.so.6 来获取线程id的方法, 当然事实证明是有效果的.

老外的连接 http://blog.devork.be/2010/09/finding-linux-thread-id-from-within.html

ctypes是Python的一个外部库,提供和C语言兼容的数据类型,可以很方便地调用C DLL中的函数. 我对这个ctypes理解也不深入,在以前的项目中用过,表示有些粗暴.

废话不多说, 直接上python ctypes样例,关于这186,224,178不知道啥意思.

1
2
3

import ctypes
for id in [186, 224, 178]:
    tid = ctypes.CDLL('libc.so.6').syscall(id)  #syscall系统调用

下面是python threading获取线程id的实例代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33

#xiaorui.cc
#coding:utf-8
import os
import threading
import ctypes
import time
import requests
def pthread_level1(i):
    print "workor id :%s"%i
    #获取threading对象的标识ident
    print threading.currentThread()
    print threading.currentThread().ident
    print "threaing id: ",ctypes.CDLL('libc.so.6').syscall(186)
    d = requests.get("http://www.google.com")
    time.sleep(100)
    return
if __name__ == "__main__":
    l = []
    for i in xrange(5):
        t = threading.Thread(target=pthread_level1,args=(i,))
        l.append(t)
    for i in l:
        i.start()
    #查看进程跟线程的关系
    os.system("pstree -p " + str(os.getpid()))
    for i in l:
        i.join()
    print "Sub-process done."

这是上面py代码运行后的结果,  跟我们预期的效果一致.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27

[ruifengyun@wx-test-social11:~]$python a.py
workor id :0
<Thread(Thread-1, started 140665607177984)>
workor id :1
140665607177984<Thread(Thread-2, started 140665596688128)>
140665596688128workor id :2
threaing id:  24828
<Thread(Thread-3, started 140665586198272)>
140665586198272
threaing id:  24829
threaing id:  workor id :3
<Thread(Thread-4, started 140665575708416)>
140665575708416
threaing id:  24830
24827
workor id :4
<Thread(Thread-5, started 140665565218560)>
140665565218560
threaing id:  24831
python(24826)─┬─pstree(24832)
              ├─{python}(24827)
              ├─{python}(24828)
              ├─{python}(24829)
              ├─{python}(24830)
              └─{python}(24831)

可以另起一个终端使用pstree -p pid看看是否正确.

1
2
3
4
5
6

[ruifengyun@wx-test-social11:~]$pstree -p 24826
python(24826)─┬─{python}(24827)
              ├─{python}(24828)
              ├─{python}(24829)
              ├─{python}(24830)
              └─{python}(24831)

那么我们费尽心思取到python的线程id是为了什么?  strace -p pid/线程 的状态.  可以看到24831线程正在建立google.com的连接, 很明显这连接被拒了.

1
2
3
4
5
6
7
8
9
10

[ruifengyun@wx-test-social11:~]$strace -p 24826
Process 24826 attached - interrupt to quit
futex(0x1abfcd0, FUTEX_WAIT_PRIVATE, 0, NULL
^C <unfinished ...>
Process 24826 detached
[ruifengyun@wx-test-social11:~]$strace -p 24828
Process 24828 attached - interrupt to quit
connect(8, {sa_family=AF_INET, sin_port=htons(80), sin_addr=inet_addr
("216.58.221.228")}, 16

END.  下次有时间在专门瞅瞅python ctypes的用法.

对Python及运维开发感兴趣的朋友可以加QQ群 : 478476595 !!! 
{ 2000人qq大群内有各厂大牛,常组织线上分享及沙龙,对高性能及分布式场景感兴趣同学欢迎加入该QQ群 }

另外如果大家觉得文章对你有些作用!   帮忙点击广告. 一来能刺激我写博客的欲望,二来好维护云主机的费用. 
如果想赏钱,可以用微信扫描下面的二维码. 另外再次标注博客原地址  xiaorui.cc  ……   感谢!

其中提到的老外的文章:

So I've got a multi-threaded application and suddenly I notice there's one thread running away and using all CPU. Not good, probably a loop gone wrong. But where? One way to find this is revert history in the VCS and keep trying it out till you find the bad commit. Another way is to find out which thread is doing this, this is of course much more fun!

Using ps -p PID -f -L you'll see the thread ID which is causing the problems. To relate this to a Python thread I subclass threading.Thread, override it's .start() method to first wrap the .run() method so that you can log the thread ID before calling the original .run(). Since I was already doing all of this apart from the logging of the thread ID this was less work then it sounds. But the hard part is finding the thread ID.

Python knows of a threading.get_ident() method but this is merely a long unique integer and does not correspond to the actual thread ID of the OS. The kernel allows you to get the thread ID: getid(2). But this must be called using a system call with the constant name SYS_gettid. Because it's hard to use constants in ctypes (at least I don't know how to do this), and this is not portable anyway, I used this trivial C program to find out the constant value:

#include <stdio.h>
#include <sys/syscall.h>int main(void)
{printf("%d\n", SYS_gettid);return 0;
}

In my case the constant to use is 186. Now all that is left is using ctypes to do the system call:

import ctypesSYS_gettid = 186
libc = ctypes.cdll.LoadLibrary('libc.so.6')
tid = libc.syscall(SYS_gettid)

That's it! Now you have the matching thread ID!

Going back to the original problem you can now associate this thread ID with the thread name and you should be able to find the problematic thread.

对于‘libc.so.6'的使用可以是直接调用或是先载入(Loadlibrary)都行。

2、采用ubuntu系统时可能会碰到libc.so.6位置的问题,即无法导入模块,或无法找到该动态库时解决方法:

在Ubuntu 14.04LTS用命令:/lib/libc.so.6时,提示” /lib/libc.so.6: not found“,其实这个库是存在的,只是地方换了,在"/lib/i386-linux-gnu/"下面,我们只需创建一个链接即可。

使用下面的命令:

For 64 bit:

sudo ln -s /lib64/x86_64-linux-gnu/libc-2.13.so /lib64/libc.so.6

For 32 bit:

sudo ln -s /lib/i386-linux-gnu/libc-2.13.so /lib/libc.so.6

http://xiaorui.cc/2016/03/21/python%E4%B8%8B%E4%BD%BF%E7%94%A8ctypes%E8%8E%B7%E5%8F%96threading%E7%BA%BF%E7%A8%8Bid/

http://blog.51cto.com/happyliu/1731402

python 中获取线程id相关推荐

  1. ceph bluestore源码分析:C++ 获取线程id

    阅读ceph源码过程中需要明确当前操作是由哪个线程发出,此时需要根据线程id来确认线程名称 C++获取线程id是通过系统调用来直接获取 函数描述 头文件:<sys/syscall.h> 函 ...

  2. Python中的线程间通信

    Python中的线程间通信 文章目录 Python中的线程间通信 1.Queue 2.同步机制 1.Event 2.Semaphore(信号量) 3.Lock(锁) 4.RLock(可重入锁) 5.C ...

  3. Java 线程实例一(查看线程是否存活、获取当前线程名称、状态监测、线程优先级设置、死锁及解决方法、获取线程id、线程挂起)

    查看线程是否存活 以下实例演示了如何通过继承 Thread 类并使用 isAlive() 方法来检测一个线程是否存活: public class TwoThreadAlive extends Thre ...

  4. 如何在Python中获取文件创建和修改日期/时间?

    我有一个脚本,该脚本需要根据文件创建和修改日期执行一些操作,但必须在Linux和Windows上运行. 在Python中获取文件创建和修改日期/时间的最佳跨平台方法是什么? #1楼 最好的功能是os. ...

  5. C# API之常用操作窗口类函数详解[查找所有窗口、获取目标句柄的类名、获取窗口文本、获取当前活动窗口、通过窗口句柄获取线程ID、获取指定窗口位置]

    /// <summary>/// 查找所有窗口(只要是在进程里面的)/// 如果不限制类名或者标题使用null代替/// </summary>/// <param nam ...

  6. python中的线程技术

    #!/user/bin/env python # @Time :2018/7/7 11:42 # @Author :PGIDYSQ #@File :DaemonTest.py import threa ...

  7. Python中获取异常(try Exception)信息

    Python中获取异常(try Exception)信息 参考文章: (1)Python中获取异常(try Exception)信息 (2)https://www.cnblogs.com/hixiao ...

  8. 在Python中获取文件大小? [重复]

    本文翻译自:Getting file size in Python? [duplicate] This question already has an answer here: 这个问题在这里已有答案 ...

  9. python停止线程池_详解python中Threadpool线程池任务终止示例代码

    需求 加入我们需要处理一串个位数(0~9),奇数时需要循环打印它:偶数则等待对应时长并完成所有任务:0则是错误,但不需要终止任务,可以自定义一些处理. 关键点 定义func函数处理需求 callbac ...

最新文章

  1. 谷歌高管又变动:AI 研究部门元老即将离职
  2. eCos中的线程与同步
  3. 函数exit()详解:参数EXIT_FAILURE(是1),EXIT_SUCCESS(是0)
  4. mqtt js 中乱码_Vue.js 中的 v-cloak 指令——Vue学习之路
  5. Java中,我自己定义的某个类,去实现某个接口,是否必须实现该接口的全部抽象方法呢?
  6. 深入了解区块链技术及其常见误区
  7. 英语口语week 14 Thursday
  8. 2、压滤机现场图片和组成部分详细构造
  9. 详解数字电视机顶盒的功能技术与应用3
  10. 《编写有效用例》阅读笔记04
  11. mac上面比较好用的软件
  12. SM2算法和RSA算法简介
  13. 渗透测试实战分享—从app到网站沦陷
  14. 你的手指上有几个螺(斗)?指纹决定你的性格
  15. 自己如何创业,参业初创公司,看领头人,看他的人品、决心和能力 .
  16. 华为HCNA之WAN接入配置实验
  17. HTML提供的5种空格实体(nbsp`;`ensp`; `emsp`;` thinsp`; `zwnj`;`zwj`;)
  18. 转发和重定向简介及与之相关的(URL)参数(parameter)、属性(attribute)问题探讨
  19. JS和jQuery通过this获取html标签中的属性值
  20. [固态硬盘保养 第3回] 电脑日常使用妙招二:打开写入缓存

热门文章

  1. Android Studio下载路径
  2. 程序员7天内面试了10家公司,如何从命中率0%到命中率至70%?
  3. 【网】Google,无处不在
  4. 广西人工智能学会第一次会员代表大会召开
  5. php 老黄历算法,老黄历用语解释
  6. android studio 7200u,轻薄有力量: HP 惠普 发布 ZBook Studio 工作站级超极本
  7. 300多款思维导图模版,助力CSDN平台的程序员们梳理技能知识点,速度收藏!
  8. ubuntu 系统安装qq2013(转)
  9. PID控制器概述及python实现PID控制算法
  10. pycharm中单行、多行注释