Python 限制线程的最大数量(Semaphore)

import threading
import time

sem = threading.Semaphore(4) # 限制线程的最大数量为4


def gothread():
with sem: # 锁定线程的最大数量
for i in range(8):
print(threading.current_thread().name, i)
time.sleep(1)


for i in range(5):
threading.Thread(target=gothread).start()

 

转载于:https://www.cnblogs.com/smxiazi/p/8878283.html

你可能感兴趣的:(Python 限制线程的最大数量(Semaphore))