python 多线程 信号量semaphore(并行编程 5)

import threading
import time
import random

semaphore = threading.Semaphore(0)

def consumer():
print("consumer is waiting.")
semaphore.acquire()
print("Consumer notify: consumed item number %s." % item)

def producer():
global item
time.sleep(10)
item = random.randint(1, 1000)
print("producer nofity: produced item number %s." % item)
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 teminated.")

threading.Semaphore(0)

se.acquire()
se.release()

你可能感兴趣的:(python 多线程 信号量semaphore(并行编程 5))