代码之家  ›  专栏  ›  技术社区  ›  Charles Anderson

如何在Python中获取线程id?

  •  268
  • Charles Anderson  · 技术社区  · 17 年前

    writeLog(message)

    writeLog() 能够向消息中添加一些内容来识别哪个线程正在调用它。显然,我可以让线程传递这些信息,但这将需要更多的工作。是否有线程等效的 os.getpid() 我可以用吗?

    8 回复  |  直到 6 年前
        1
  •  356
  •   Asclepius    7 年前

    threading.get_ident() 作品,或 threading.current_thread().ident (或 threading.currentThread().ident 对于Python< 2.6).

        2
  •  96
  •   Community Mohan Dere    6 年前

    使用 logging 模块,您可以在每个日志条目中自动添加当前线程标识符。 只需使用其中一个 LogRecord 在日志格式字符串中映射按键:

    %(螺纹)d: 线程ID(如果可用)。

    线程名称(如果可用)。

    并用它设置默认处理程序:

    logging.basicConfig(format="%(threadName)s:%(message)s")
    
        4
  •  32
  •   Peter Mortensen Pieter Jan Bonestroo    9 年前

    这 thread.get_ident()

    我用 this method 要在Linux上真正获取线程id:

    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)
    
        5
  •  21
  •   DeaD_EyE    7 年前

    您可以获取当前正在运行的线程的标识。如果当前线程结束,则标识可以重新用于其他线程。

    当你创建一个Thread实例时,会给线程一个隐式的名称,即模式:线程编号

    名称没有意义,名称不必是唯一的。所有正在运行的线程的标识都是唯一的。

    import threading
    
    
    def worker():
        print(threading.current_thread().name)
        print(threading.get_ident())
    
    
    threading.Thread(target=worker).start()
    threading.Thread(target=worker, name='foo').start()
    

        6
  •  7
  •   miku    17 年前

    我看到了这样的线程ID示例:

    class myThread(threading.Thread):
        def __init__(self, threadID, name, counter):
            self.threadID = threadID
            ...
    

    这 threading module docs 列表 name

    ...
    
    A thread has a name. 
    The name can be passed to the constructor, 
    and read or changed through the name attribute.
    
    ...
    
    Thread.name
    
    A string used for identification purposes only. 
    It has no semantics. Multiple threads may
    be given the same name. The initial name is set by the constructor.
    
        7
  •  0
  •   Peter Mortensen Pieter Jan Bonestroo    9 年前

    我在Python中创建了多个线程,打印了线程对象,并使用 ident 变量。我看到所有的id都是一样的:

    <Thread(Thread-1, stopped 140500807628544)>
    <Thread(Thread-2, started 140500807628544)>
    <Thread(Thread-3, started 140500807628544)>
    
        8
  •  0
  •   kirr    8 年前

    与@bruchexin类似,我需要获取操作系统级线程标识符(哪= thread.get_ident() )并使用以下内容,不依赖于特定数字,仅限amd64:

    ---- 8< ---- (xos.pyx)
    """module xos complements standard module os""" 
    
    cdef extern from "<sys/syscall.h>":                                                             
        long syscall(long number, ...)                                                              
        const int SYS_gettid                                                                        
    
    # gettid returns current OS thread identifier.                                                  
    def gettid():                                                                                   
        return syscall(SYS_gettid)                                                                  
    

    和

    ---- 8< ---- (test.py)
    import pyximport; pyximport.install()
    import xos
    
    ...
    
    print 'my tid: %d' % xos.gettid()
    

    但这取决于Cython。

    推荐文章