一.线程的其他方法和属性

from threading import current_thread    # 导入当前线程模块

1.方法

  current_thread().getName():获取当前线程名称

 

  threading.enumerate():获取所有运行中的线程状态,返回一个列表

  threading.active_count():获取正在运行的线程数量

2.属性

  current_thread().ident:获取当前线程id

import threading
from threading import Thread,current_thread   # 当前的线程

def f1():
    print(current_thread().ident)   # 当前的线程id
    print("线程一号")

if __name__ == '__main__':
    t = Thread(target=f1,)
    t.start()
    print(current_thread().getName())   # 当前的线程名称
    print(current_thread().ident)
    print(threading.enumerate())   # 枚举 返回一个所有运行中的线程状态[<_MainThread(MainThread, started 2756)>]
    print(threading.active_count())  # 返回所有运行的线程个数
current_thread模块中的方法,属性

相关文章:

  • 2021-07-29
  • 2022-12-23
  • 2021-05-23
  • 2021-07-27
  • 2021-11-17
  • 2021-07-25
  • 2021-05-24
猜你喜欢
  • 2021-09-27
  • 2021-12-13
  • 2022-12-23
  • 2021-12-14
  • 2021-08-20
  • 2022-01-23
  • 2021-06-22
相关资源
相似解决方案