如何在 Python 中获取线程 ID? - How to obtain a Thread id in Python?

10/14 13:41
阅读数 13

问题:

I have a multi-threading Python program, and a utility function, writeLog(message) , that writes out a timestamp followed by the message.我有一个多线程 Python 程序和一个实用程序函数writeLog(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.我希望writeLog()能够在消息中添加一些内容以识别哪个线程正在调用它。 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?是否有一些与os.getpid()等效的线程可以使用?


解决方案:

参考一: https://en.stackoom.com/question/3rJ3
参考二: https://stackoom.com/question/3rJ3
展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部