python,信号量,semaphore
python中的信号量,是通过定义semaphore对象,控制同时可以运行的线程的数量,同时也是一种锁,下面的代码演示了信号量的应用
import threading,time class MyThread(threading.Thread): def __init__(self,name): threading.Thread.__init__(self) self.name = name def run(self): semaphore.acquire() print(self.name) time.sleep(3) semaphore.release() if __name__ == "__main__": semaphore = threading.BoundedSemaphore(5) for i in range(40): t = MyThread(‘Alex‘) t.start()这段代码运行后,通过semaphore实现了同时可以允许5个线程同步运行,每隔3秒显示5个‘Alex‘
相关推荐
starinshy 2020-11-10
神龙 2020-06-07
hahalale 2019-11-30
wordmhg 2019-11-30
GeorgeTH 2019-10-29
playlinuxxx 2016-07-30
sunzxh 2020-03-23
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