python - 如何在 Python 中获取线程 id?

我有一个多线程 Python 程序和一个实用函数 writeLog(message),它会在消息之后写出时间戳。不幸的是,生成的日志文件没有说明哪个线程正在生成哪个消息。

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

最佳答案

threading.get_ident()作品,或threading.current_thread().ident (或 threading.currentThread().ident 用于 Python

https://stackoverflow.com/questions/919897/

相关文章:

linux - Unix:如何删除文件中列出的文件

c - 如何在linux c程序中获取pthread的线程ID?

python - 在几个文件中拆分views.py

linux - shell脚本杀死监听端口3000的进程?

python - 在 IPython 中自动重新加载模块

python - os.path.dirname(__file__) 返回空

python - 如何为子图设置 xlim 和 ylim

c - dup2/dup - 为什么我需要复制文件描述符?

python - 如何在 Python 中进行 scp?

python - 打印 Python 类的所有属性