在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
辰迅云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
推荐阅读: python中怎么用plt.hist做柱状图