Concurrent Programming semaphore

signal

  • It does not solve security problems, but to limit the maximum amount of concurrent
# *************信号量,限制同时执行代码的线程数量**************

from threading import Semaphore,currentThread,Thread
import time


s = Semaphore(5)        # 限制同时执行代码的线程数量为5,如果为1,就是一个普通的互斥锁


def task():
    s.acquire()
    time.sleep(1)
    print(currentThread().name)
    s.release()

for i in range(10):
    Thread(target=task).start()
Thread-2
Thread-1
Thread-5
Thread-4
Thread-3
Thread-7
Thread-9
Thread-8
Thread-10
Thread-6

Guess you like

Origin www.cnblogs.com/plf-Jack/p/11139829.html