介绍
Thread实例对象的方法 # isAlive(): 返回线程是否活动的。 # getName(): 返回线程名。 # setName(): 设置线程名。threading模块提供的一些方法: # threading.currentThread(): 返回当前的线程变量。 # threading.enumerate(): 返回一个包含正在运行的线程的list。正在运行指线程启动后、结束前,不包括启动前和终止后的线程。 # threading.activeCount(): 返回正在运行的线程数量,与len(threading.enumerate())有相同的结果。
current_thread 获取当前线程对象 getName() 获取当前线程名字
from threading import Threadfrom threading import current_threadimport time# current_thread 获取当前线程对象名字# getName() 获取当前线程名字def task(): print("%s is running" % current_thread().getName()) time.sleep(2) print("%s is done" % current_thread().getName())if __name__ == '__main__': t = Thread(target=task,) t.start()'''Thread-1 is runningThread-1 is done'''
默认名字是Thread-1
getName() 获取当前线程名字,t就是current_thread() 当前线程的对象
from threading import Thread, current_threadimport time# current_thread 获取当前线程对象名字def task(): print("%s is running" % current_thread().getName()) time.sleep(2) print("%s is done" % current_thread().getName())if __name__ == '__main__': t = Thread(target=task,) t.start() print(t.getName()) # = current_thread().getName()'''Thread-1 is runningThread-1Thread-1 is done'''
主线程名字默认是MainThread
from threading import Thread, current_threadimport time# current_thread 获取当前线程对象名字def task(): print("%s is running" % current_thread().getName()) time.sleep(2) print("%s is done" % current_thread().getName())if __name__ == '__main__': t = Thread(target=task,) t.start() print("主线程", current_thread().getName()) # 打印主线程名字'''Thread-1 is running主线程 MainThreadThread-1 is done'''
改子线程名字 setName()
from threading import Thread, current_threadimport time# current_thread 获取当前线程对象名字def task(): print("%s is running" % current_thread().getName()) time.sleep(2) print("%s is done" % current_thread().getName())if __name__ == '__main__': # name 改子线程名字 t = Thread(target=task, name="子线程1") t.start() # 改子线程名字 t.setName("儿子线程1") print("主线程", current_thread().getName()) # 打印主线程名字'''子线程1 is running主线程 MainThread儿子线程1 is done'''
启动程序瞬间开启子线程
改主线程名字
current_thread.setName()
from threading import Thread, current_threadimport time# current_thread 获取当前线程对象名字def task(): print("%s is running" % current_thread().getName()) time.sleep(2) print("%s is done" % current_thread().getName())if __name__ == '__main__': t = Thread(target=task, name="子线程1") t.start() current_thread().setName("主线程helo") print("主线程", current_thread().getName())'''子线程1 is running主线程 主线程helo子线程1 is done'''
t.isAlive() 查看子进程是否存活
from threading import Thread, current_threadimport time# current_thread 获取当前线程对象名字def task(): print("%s is running" % current_thread().getName()) time.sleep(2) print("%s is done" % current_thread().getName())if __name__ == '__main__': t = Thread(target=task, name="子线程1") t.start() # 判断子线程是否存活 print(t.isAlive()) print("主线程")'''子线程1 is runningTrue主线程子线程1 is done'''
主线程等待子线程运行完,主线程再执行 join()
from threading import Thread, current_threadimport time# current_thread 获取当前线程对象名字def task(): print("%s is running" % current_thread().getName()) time.sleep(2) print("%s is done" % current_thread().getName())if __name__ == '__main__': t = Thread(target=task, name="子线程1") t.start() print(t.isAlive()) t.join() # 判断子线程是否存活 print("主线程") print(t.isAlive())'''子线程1 is runningTrue子线程1 is done主线程False'''
activeCount(): 返回正在运行的线程数量
from threading import Thread, current_thread, active_countimport time# current_thread 获取当前线程对象名字def task(): print("%s is running" % current_thread().getName()) time.sleep(2) print("%s is done" % current_thread().getName())if __name__ == '__main__': t = Thread(target=task, name="子线程1") t.start() t.join() print("主线程") # 返回正在运行的线程数量 print(active_count())'''子线程1 is running子线程1 is done主线程1只剩下主线程'''
enumerate() 返回一个包含正在运行的线程的列表list。正在运行指线程启动后、结束前,不包括启动前和终止后的线程。
from threading import Thread, current_thread, enumerateimport time# current_thread 获取当前线程对象名字def task(): print("%s is running" % current_thread().getName()) time.sleep(2) print("%s is done" % current_thread().getName())if __name__ == '__main__': t = Thread(target=task, name="子线程1") t.start() print("主线程") # 返回一个包含正在运行的线程的list。正在运行指线程启动后、结束前,不包括启动前和终止后的线程。 print(enumerate())'''子线程1 is running主线程[<_MainThread(MainThread, started 38668)>,]子线程1 is done'''