如何在Python中获取线程id?
我有一个多线程 Python 程序和一个实用函数 writeLog(message)
,它写出时间戳,后跟消息。 不幸的是,生成的日志文件没有给出哪个线程正在生成哪个消息的指示。
我希望 writeLog() 能够在消息中添加一些内容来识别哪个线程正在调用它。 显然我可以让线程传递这些信息,但这需要更多的工作。 我可以使用一些与 os.getpid() 等效的线程吗?
I have a multi-threading Python program, and a utility function, writeLog(message)
, that writes out a timestamp followed by the message. Unfortunately, the resultant log file gives no indication of which thread is generating which message.
I would like writeLog()
to be able to add something to the message to identify which thread is calling it. Obviously I could just make the threads pass this information in, but that would be a lot more work. Is there some thread equivalent of os.getpid()
that I could use?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
threading.get_ident()
有效,或threading.current_thread().ident
< /a> (或threading.currentThread().ident
对于 Python <2.6)。threading.get_ident()
works, orthreading.current_thread().ident
(orthreading.currentThread().ident
for Python < 2.6).使用 logging 模块,您可以自动在每个日志条目中添加当前线程标识符。
只需在记录器格式字符串中使用以下 LogRecord 映射键之一:
并用它设置你的默认处理程序:
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:
and set up your default handler with it:
Python 3.8+ 现在支持此功能:)
您现在可以使用:
threading.get_native_id()
https://github.com/python/cpython/commit/4959c33d2555b89b494c678d99be81a65ee864b0
https://github.com/python/cpython/pull/11993
This functionality is now supported by Python 3.8+ :)
You can now use:
threading.get_native_id()
https://github.com/python/cpython/commit/4959c33d2555b89b494c678d99be81a65ee864b0
https://github.com/python/cpython/pull/11993
threading.get_ident()
函数在 Linux 上返回一个长整数。 它并不是真正的线程 ID。我使用这个方法来真正在 Linux 上获取线程 id:
The
threading.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:
您可以获取当前正在运行的线程的标识。 如果当前线程结束,则该标识可以重用于其他线程。
当您创建 Thread 的实例时,会为线程隐式指定一个名称,其模式如下: Thread-number
该名称没有任何意义,并且该名称不必是唯一的。 所有正在运行的线程的标识都是唯一的。
函数 threading.current_thread() 返回当前正在运行的线程。 该对象保存了线程的全部信息。
You can get the ident of the current running thread. The ident could be reused for other threads, if the current thread ends.
When you crate an instance of Thread, a name is given implicit to the thread, which is the pattern: Thread-number
The name has no meaning and the name don't have to be unique. The ident of all running threads is unique.
The function threading.current_thread() returns the current running thread. This object holds the whole information of the thread.
我看到了这样的线程 ID 示例:
线程模块文档还列出了
name
属性:I saw examples of thread IDs like this:
The threading module docs lists
name
attribute as well:我在 Python 中创建了多个线程,打印了线程对象,并使用
ident
变量打印了 id。 我看到所有的 id 都是相同的:I created multiple threads in Python, I printed the thread objects, and I printed the id using the
ident
variable. I see all the ids are same:与@brucexin类似,我需要获取操作系统级线程标识符(其中!=
thread.get_ident()
)并使用类似下面的内容来不依赖于特定数字并且仅限amd64:这
取决于不过在 Cython 上。
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:and
this depends on Cython though.