import threading
import time
import queue
def consume(thread\\_name, q):
while True:
time.sleep(2)
product = q.get()
print("%s consume %s" % (thread\\_name, product))
q.task\\_done()
def produce(thread\\_name, q):
for i in range(3):
product = 'product-' + str(i)
q.put(product)
print("%s produce %s" % (thread\\_name, product))
time.sleep(1)
q.join()
q = queue.Queue()
p = threading.Thread(target=produce, args=("producer",q))
c = threading.Thread(target=consume, args=("consumer",q))
c1 = threading.Thread(target=consume, args=("consumer-1",q))
c.setDaemon(True)
c1.setDaemon(True)
p.start()
c.start()
c1.start()
p.join()
import threading
import time
import queue
def consume(thread_name, q):
while True:
time.sleep(2)
product = q.get()
print("%s consume %s" % (thread_name, product))
def produce(thread_name, q):
for i in range(3):
product = 'product-' + str(i)
q.put(product)
print("%s produce %s" % (thread_name, product))
time.sleep(1)
q = queue.Queue()
p = threading.Thread(target=produce, args=("producer",q))
c = threading.Thread(target=consume, args=("consumer",q))
p.start()
c.start()
p.join()
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。