租用问题

质量为本、客户为根、勇于拼搏、务实创新

< 返回租用问题列表

python如何查看当前线程id,查看python函数

发布时间:2023-10-27 20:37:59

python如何查看当前线程id

在Python中,可使用threading模块的current_thread()函数来获得当前线程的ID。具体操作以下:

import threading

# 定义一个函数,用于打印当前线程的ID
def print_current_thread_id():
    thread_id = threading.current_thread().ident
    print("当前线程的ID为:", thread_id)

# 在主线程中调用函数
print_current_thread_id()

# 创建一个新线程并调用函数
thread = threading.Thread(target=print_current_thread_id)
thread.start()
thread.join()

输出结果以下:

当前线程的ID为: 140034731632448
当前线程的ID为: 140034715238144