立即學習:https://edu.csdn.net/course/play/24458/296446?utm_source=blogtoedu
信號量(了解):也是一把鎖semaphore
?
1.
from threading import Thread,Semaphore,currentThread
import time#定義信號量(3把鎖)
sm = Semaphore(3)def task():with sm:print('%s acquires the sm' % currentThread().getName())time.sleep(1)if __name__ == '__main__':for i in range(10):t = Thread(target=task)t.start()
?
2.
sm.acquire()
print('%s acquires the sm'%currentThread().getName())
sm.release()#等價于
with sm:print('%s acquires the sm'%currentThread().getName())