【python】threading模块之Semaphore

发布时间 2023-06-06 19:54:40作者: 苏格拉底的落泪

 

threading模块之Semaphore

import threading
import time
import random

# The optional argument gives the initial value for the internal counter;
# it defaults to 1.
# If the value given is less than 0, ValueError is raised.
semaphore = threading.Semaphore(0)

def consumer():
    print("consumer is waiting.")
    # Acquire a semaphore
    semaphore.acquire()
    # The consumer have access to the shared resource
    print("Consumer notify : consumed item number %s " % item)

def producer():
    global item
    time.sleep(10)
    # create a random item
    item = random.randint(0, 1000)
    print("producer notify : produced item number %s" % item)
        # Release a semaphore, incrementing the internal counter by one.
    # When it is zero on entry and another thread is waiting for it
    # to become larger than zero again, wake up that thread.
    semaphore.release()

if __name__ == '__main__':
    for i in range (0,5) :
            t1 = threading.Thread(target=producer)
            t2 = threading.Thread(target=consumer)
            t1.start()
            t2.start()
            t1.join()
            t2.join()
    print("program terminated")

输出:

 

 

参考资料

1. 使用信号量进行线程同步