import threading
import time
def tt(t):
# 开启线程限制
with pool_sema:
print(t)
time.sleep(2)
if __name__ == '__main__':
# 并发的线程数设置
thread_nums = 2
pool_sema = threading.BoundedSemaphore(value=thread_nums)
threads = []
for i in range(1, 20):
th = threading.Thread(target=tt, args=(i,))
th.start()
threads.append(th)
for th in threads:
th.join()
python的threading模块限制线程并发数
最新推荐文章于 2024-05-13 09:05:05 发布
关键词由CSDN通过智能技术生成