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
Using the logging module you can automatically add the current thread identifier in each log entry. Just use one of these LogRecord mapping keys in your logger format string:
%(thread)d : Thread ID (if available).
%(threadName)s : Thread name (if available).
and set up your default handler with it:
logging.basicConfig(format="%(threadName)s:%(message)s")
This functionality is now supported by Python 3.8+ :)
https://github.com/python/cpython/commit/4959c33d2555b89b494c678d99be81a65ee864b0
https://github.com/python/cpython/pull/11993
I saw examples of thread IDs like this:
class myThread(threading.Thread):
def __init__(self, threadID, name, counter):
self.threadID = threadID
...
The threading module docs lists name
attribute as well:
...
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.
Similarly to @brucexin I needed to get OS-level thread identifier (which != thread.get_ident()
) and use something like below not to depend on particular numbers and being amd64-only:
---- 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)
and
---- 8< ---- (test.py)
import pyximport; pyximport.install()
import xos
...
print 'my tid: %d' % xos.gettid()
this depends on Cython though.
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)
threading.get_ident() works, or threading.current_thread().ident (or threading.currentThread().ident
for Python < 2.6).