生产者-消费者模型-线程安全队列Queue - 好的例子 - 协程实现生产者消费者模型

#python3
#product new data into the queue
#comsume data from the queue
from queue import Queue
import time , threading
class product_data(threading.Thread):
    def __init__(self,name,queue):
        threading.Thread.__init__(self,name=name)
        self.data = queue
    def run(self):
        print('start product___')
        for i in range(5):
            print('create new data: {0}'.format(i))
            self.data.put('put new data:{0} '.format(i,self.name))
            time.sleep(2)
        print('put data finished')
        return self.data
class comsume_data(threading.Thread):
    def __init__(self,name,queue):
        threading.Thread.__init__(self,name=name)
        self.data = queue
    def run(self):
        print('start get data')
        for i in range(5):
            small_data = self.data.get()
            print('get data:{0} from {1}'.format(small_data,self.name))
            time.sleep(5)
        print('get data finished')

def main():
    queue = Queue()
    new_product = product_data('pro_fuck',queue)
    new_comsume = comsume_data('com_fuck',queue)

    new_product.start()
    new_comsume.start()

    new_product.join()
    new_comsume.join()

if __name__ == '__main__':
    main()
参考:http://python.jobbole.com/87592/

 

简单案例:

#!/usr/bin/python
import time, threading,Queue
#class to do sth
class Comsumer(threading.Thread):
def __init__(self,queue):
  threading.Thread.__init__(self)
  self.queue = queue
def run(self):
  while True:
    msg = self.queue.get()
    if msg == 'quit':
      break
    print 'msg is : {0}'.format(msg)
    time.sleep(1)
  print 'good bye'

def producer():
  queue = Queue.Queue()
  worker = Comsumer(queue)
  worker.start()# 开启消费者线程


  for i in range(5):
    queue.put('queue-{0}'.format(i))
  queue.put('quit')
  worker.join() #不是queue.join()

if __name__ == '__main__':
  producer()

 

好的例子:

https://cloud.tencent.com/developer/article/1047257

 

4. asyncio - 生产者消费者模型

import asyncio
import random
import time

# comsumer get val from queue
async def my_comsumer(queue, id):
          while True:
              print('   --   my_comsumer : {} called'.format(id))
              val = await queue.get()
              print('my_comsumer id: {} , val: {}'.format(id, val))
              await asyncio.sleep(4)



# producer put val from queue
async def my_producer(queue, id):
        print('  --  my_producer : {} called'.format(id))
        for i in range(1,4):
              val = i
              await queue.put(val)
              print('my_producer id: {} , val: {}'.format(id, val))
              await asyncio.sleep(1)



async def main():
          queue = asyncio.Queue()
          my_producer_task1 = asyncio.create_task(my_producer(queue, 'my_producer_task1'))
          my_producer_task2 = asyncio.create_task(my_producer(queue, 'my_producer_task2'))
          my_comsumer_task1 = asyncio.create_task(my_comsumer(queue, 'my_comsumer_task1'))
          my_comsumer_task2 = asyncio.create_task(my_comsumer(queue, 'my_comsumer_task2'))

          await asyncio.sleep(10)  # 这里有阻塞,事件循环控制器交给其他await

          my_comsumer_task1.cancel() # await执行完,继续执行主进程任务
          my_comsumer_task2.cancel()
          await asyncio.gather(my_producer_task1, my_producer_task2, my_comsumer_task1, my_comsumer_task2,return_exceptions=True)   # 代替try execpt来捕获报错


if __name__ == '__main__':
    start_time = time.perf_counter()
    asyncio.run(main())
    end_time = time.perf_counter()
    print('main duration: {}'.format(end_time - start_time))

 

posted @ 2018-05-27 16:35  littlevigra  阅读(263)  评论(1)    收藏  举报