扫描二维码下载沐宇APP

沐宇

微信扫码使用沐宇小程序

沐宇

python怎么查看当前线程id

扬州沐宇科技
2023-10-27 20:37:59
python

在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

扫码添加客服微信