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() 相关推荐
神龙 2020-06-07
GeorgeTH 2019-10-29
playlinuxxx 2016-07-30
sunzxh 2020-03-23
laityc 2019-12-15
hahalale 2019-11-30
wordmhg 2019-11-30
Happyunlimited 2019-11-16
gongruitao 2019-10-30
zhaojiacan 2018-07-12
mojianc 2019-06-28
peterlee 2017-09-29
Will0 2011-04-10
honghao0 2010-03-17
pythoncream 2019-04-17
ytp00ytp 2019-04-16
稀土 2018-05-11
友心人 2018-05-07