
说明
1、threading.curentthread():返回当前线程变量。
2、threading.enumerate():返回包含正在运行的线程的list。指线程启动后和结束前不包括启动前和结束后的线程。
threading.activeCount():与len(threading.enumerate()有相同的结果。
实例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | "" "
python threading模块的常用方法
" ""
import time
import threading
def test1():
print( '------test1-------' )
time.sleep(3)
def test2():
print( '------test2-------' )
time.sleep(3)
def main():
t1 = threading.Thread(target=test1)
t2 = threading.Thread(target=test2)
t1.start()
t2.start()
print( 'activeCount: %d' % threading.activeCount())
print(threading.enumerate())
while threading.activeCount() != 1:
time.sleep(1)
print(threading.enumerate())
print(threading.enumerate())
if __name__ == '__main__' :
main()
|
以上就是Python threading模块的常用方法,希望对大家有所帮助。更多Python学习指路:python基础教程
本文教程操作环境:windows7系统、Python 3.9.1,DELL G3电脑。