标签:技术分享 process alt bbb logs rgs log -- gevent
from multiprocessing import Process,Queue import time,random def producer(name,q): for i in range(1,11): ret = "泔水%s"%i q.put(ret) print("厨师%s生产了%s" % (name, ret)) time.sleep(random.randint(0,2)) def customer(name,q): while True: ret = q.get() if ret ==None:break time.sleep(random.randint(1,2)) print("顾客%s吃了%s"%(name,ret)) if __name__ == ‘__main__‘: q =Queue() p = Process(target=producer,args=("egon",q)) c = Process(target=customer,args=("alex",q)) p.start() c.start() p.join() q.put(None) c.join() print("主进程")
from threading import Thread,current_thread import queue import time,random def producer(name,q): for i in range(1,11): ret = "泔水%s"%i q.put(ret) print("厨师%s生产了%s" % (name, ret),current_thread().getName()) time.sleep(random.randint(0,2)) def customer(name,q): while True: ret = q.get() if ret ==None:break time.sleep(random.randint(1,2)) print("顾客%s吃了%s"%(name,ret),current_thread().getName()) if __name__ == ‘__main__‘: q =queue.Queue() p = Thread(target=producer,args=("egon",q)) c = Thread(target=customer,args=("alex",q)) p.start() c.start() p.join() q.put(None) c.join() print("主线程")
from gevent import monkey;monkey.patch_all() import gevent import queue import threading import time,random def producer(name,q): for i in range(1,11): ret = "泔水%s"%i q.put(ret) print("厨师%s生产了%s" % (name, ret),threading.current_thread().getName()) time.sleep(random.randint(0,2)) def customer(name,q): while True: ret = q.get() if ret ==None:break time.sleep(random.randint(1,2)) print("顾客%s吃了%s"%(name,ret),threading.current_thread().getName()) if __name__ == ‘__main__‘: q =queue.Queue() g1 = gevent.spawn(producer,"egon",q) g2 = gevent.spawn(customer,"alex",q) g1.join() q.put(None) g2.join() print("主线程",threading.current_thread().getName())
生产者消费者模型----------基于多进程、多线程、单线程并发
标签:技术分享 process alt bbb logs rgs log -- gevent
原文地址:http://www.cnblogs.com/houangpy-7/p/7701202.html