python 生产消费者_Python的生产者消费者模式

#!python

# -*- coding: utf-8 -*-

"""

Description: 生产者/消费者模式的模块

File: thread_worker.py

Authors: kanpiaoxue

Date: 2015年11月12日 下午5:51:08

"""

from Queue import Queue

import logging

import threading

import time

import logsetting

class TaskProtocol(object):

"""

执行任务协议类

"""

def execute(self):

"""

执行任务

"""

pass

class ProduceTasksProtocol(object):

"""

产出运行任务(TaskProtocol)的协议类

"""

def getTasks(self):

"""

产出运行的任务(TaskProtocol)列表

@return: 任务列表

"""

pass

class Producer(threading.Thread):

"""

生产者消/费者模式:生产者类

"""

def __init__(self, name, produceTasksProtocol, queue, sleepTime = 60):

"""

@param name: 线程名称

@param produceTasksProtocol: 生成 任务列表的协议类

@param queue: 运行队列

@param sleepTime: 生产者两次运行之间的间隔时间,单位:秒

"""

threading.Thread.__init__(self, name = name)

self.threadStop = False

self.name = name

self.produceTasksProtocol = produceTasksProtocol

self.queue = queue

self.sleepTime = int(sleepTime)

def run(self):

"""

线程运行方法

"""

logging.info('thread %s start to work.' % self.name)

while not self.threadStop:

tasks = self.produceTasksProtocol.getTasks()

size = str(len(tasks))

logging.info('produce %s tasks.' % size)

for task in tasks:

self.queue.put(task)

logging.info('%s was put into queue.' % task)

logging.info('%s will sleep %s.' % (self.name, self.sleepTime))

time.sleep(self.sleepTime)

def stop(self):

"""

停止线程运行

"""

self.threadStop = True

class Consumer(threading.Thread):

"""

生产者消/费者模式:消费者类

"""

def __init__(self, name, queue):

"""

@param name: 线程名称

@param queue: 运行队列

"""

threading.Thread.__init__(self, name = name)

self.threadStop = False

self.name = name

self.queue = queue

def run(self):

"""

线程运行方法

"""

logging.info('thread %s start to work.' % (self.name))

while not self.threadStop:

task = self.queue.get()

logging.info('%s was take from queue and to work.' % task)

start = time.clock()

try:

task.execute()

except Exception as error:

# 捕获异常,并记录错误日志。防止因为一个任务的失败造成线程退出工作

logging.error('when execute task, Occur some exception: %s' % error)

end = time.clock()

logging.info('%s finish working. It consumes %s seconds.' % (task,str((end - start))))

def stop(self):

"""

停止线程运行

"""

self.threadStop = True

if __name__ == '__main__':

logsetting.init_log("./log/ctapi")

class TaskProtocol_01(TaskProtocol):

"""

执行任务协议类

"""

def __init__(self, num):

TaskProtocol.__init__(self)

self.num = num

def execute(self):

"""

执行任务

"""

print self, 'start to work'

def __str__(self):

return 'task-' + str(self.num)

class ProduceTasksProtocol_01(ProduceTasksProtocol):

"""

产出运行任务(TaskProtocol)的协议类

"""

def __init__(self, count):

ProduceTasksProtocol.__init__(self)

self.count = count

def getTasks(self):

"""

产出运行的任务(TaskProtocol)列表

@return: 任务列表

"""

return [TaskProtocol_01(i) for i in xrange(count)]

count = 100

produceTasksProtocol = ProduceTasksProtocol_01(count)

queue = Queue()

producer = Producer('producer', produceTasksProtocol, queue,sleepTime=5)

producer.start()

consumerCount = 5

for x in range(consumerCount) :

consumer = Consumer('consumer-' + str(x), queue)

consumer.start()

threading.current_thread().join()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值