老是忘了 t1 = threading.Thread(target=函数名) 的写法,一会儿小写一会儿大写,简直气人,记录下
import time, threading
def sing():
for i in range(3):
print("is singing... %d" % i)
time.sleep(1)
def dance(name):
for i in range(3):
print(name + "is dancing... %d" % i)
time.sleep(1)
if __name__ == '__main__':
thread_list = threading.enumerate()
print("\nCurrent number of threads:%d" % len(thread_list))
t1 = threading.Thread(target=sing)
t2 = threading.Thread(target=dance,args=("ppx",)) # 注意参数后的这个逗号
t1.start()
t2.start()
thread_list = threading.enumerate()
print("\n获取当前运行线程名称" + str(thread_list))
print("\n获取当前运行线程数量 :%d" % len(thread_list))
输出
Current number of threads:1
is singing... 0
ppxis dancing... 0
获取当前运行线程名称[<_MainThread(MainThread, started 10312)>, <Thread(Thread-1, started 4088)>, <Thread(Thread-2, started 12688)>]
获取当前运行线程数量 :3
is singing... 1
ppxis dancing... 1
is singing... 2
ppxis dancing... 2
进程已结束,退出代码 0