How to obtain a Thread id in Python?

后端 未结 8 826
时光说笑
时光说笑 2020-12-02 08:01

I have a multi-threading Python program, and a utility function, writeLog(message), that writes out a timestamp followed by the message. Unfortunately, the resu

8条回答
  •  栀梦
    栀梦 (楼主)
    2020-12-02 08:45

    The thread.get_ident() function returns a long integer on Linux. It's not really a thread id.

    I use this method to really get the thread id on Linux:

    import ctypes
    libc = ctypes.cdll.LoadLibrary('libc.so.6')
    
    # System dependent, see e.g. /usr/include/x86_64-linux-gnu/asm/unistd_64.h
    SYS_gettid = 186
    
    def getThreadId():
       """Returns OS thread id - Specific to Linux"""
       return libc.syscall(SYS_gettid)
    

提交回复
热议问题