python 并发编程 多线程 Thread对象的其他属性或方法
介绍
Thread实例对象的方法 # isAlive(): 返回线程是否活动的。 # getName(): 返回线程名。 # setName(): 设置线程名。 threading模块提供的一些方法: # threading.currentThread(): 返回当前的线程变量。 # threading.enumerate(): 返回一个包含正在运行的线程的list。正在运行指线程启动后、结束前,不包括启动前和终止后的线程。 # threading.activeCount(): 返回正在运行的线程数量,与len(threading.enumerate())有相同的结果。
current_thread 获取当前线程对象
getName() 获取当前线程名字
from threading import Thread from threading import current_thread import 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 running Thread-1 is done '''
默认名字是Thread-1
getName() 获取当前线程名字,t就是current_thread() 当前线程的对象
from threading import Thread, current_thread import 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 running Thread-1 Thread-1 is done '''
主线程名字默认是MainThread
from threading import Thread, current_thread import 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 主线程 MainThread Thread-1 is done '''
改子线程名字
setName()
from threading import Thread, current_thread import 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_thread import 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_thread import 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 running True 主线程 子线程1 is done '''
主线程等待子线程运行完,主线程再执行 join()
from threading import Thread, current_thread import 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 running True 子线程1 is done 主线程 False '''
activeCount(): 返回正在运行的线程数量
from threading import Thread, current_thread, active_count import 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, enumerate import 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)>, <Thread(子线程1, started 39424)>] 子线程1 is done '''