python threading 多线程
程序员文章站
2024-03-03 16:49:22
...
举例:
import threading
def thread_func(x):
print('%d\n' % (x*100))
threads = []
for i in range(5): #5个线程
threads.append(threading.Thread(target = thread_func, args = (100,)))#100后面一定要有逗号,否则报错
for thread in threads:
thread.start() #线程开始
for thread in threads:
thread.join() #线程结束
thread.join()是等待结束,等5个线程都运行结束再退出当前进程;否则在线程很多的情况下很有可能出现线程还没结束进程就先退出了的情况
输出结果:
10000
10000
10000
10000
10000