Python 获取匹配 os /proc/[pid]/ 的 TID

Python get TID that matches os /proc/[pid]/

我正在尝试获取可用于访问 /proc/[tid]/sched 的线程 ID (tid)。我可以在 htop 的 PID 列中查找它,但是当我尝试从 python 内部访问它时,我不断得到 -1.

#!/usr/bin/env python3
import ctypes
import threading 

def get_tid():
    libc = ctypes.cdll.LoadLibrary('libc.so.6')
    print(libc.syscall(224))

threading.Thread(target=get_tid).run()

我在 Ubuntu 18.04

系统调用 186
import ctypes

def get_tid():
    """System call gettid on Linux, returning thread-id."""
    return ctypes.CDLL('libc.so.6').syscall(186)

Python 3.8 介绍 threading.get_native_id()

Return the native integral Thread ID of the current thread assigned by the kernel. This is a non-negative integer. Its value may be used to uniquely identify this particular thread system-wide (until the thread terminates, after which the value may be recycled by the OS).

Availability: Windows, FreeBSD, Linux, macOS, OpenBSD, NetBSD, AIX.