Quantcast
Channel: CodeSection,代码区,Python开发技术文章_教程 - CodeSec
Viewing all articles
Browse latest Browse all 9596

python如何查看线程id

$
0
0

最近使用python多线程程序,发现会耗尽所有的cpu,有必要让python程序运行在一个cpu上.,但是发现threading.get_ident() 返回一个极大的整数,根本和线程号对不上.

只是使用先获取系统调用号

点击( 此处 )折叠或打开

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

得到 186

点击( 此处 )折叠或打开

# ! / usr / bin / env python import threading import os import ctypes def cycle_burner ( ) : SYS_gettid = 186 libc = ctypes . cdll . LoadLibrary ( 'libc.so.6' ) tid = libc . syscall ( SYS_gettid ) print tid while True : meh = 84908230489 % 323422 for i in range ( 3 ) : thread = threading . Thread ( target = cycle_burner ) print "Starting a thread" thread . start ( )

运行一切ok


Viewing all articles
Browse latest Browse all 9596

Trending Articles