1、启动线程定时产生数据
2、将数据放入queue队列
3、启动一定数量的线程作为消耗,一直等待从queue队列中获取数据进行处理
主进程:维护线程池、生产者线程定时产生数据放入queue队列
‘’‘mythreadpool.py’‘’
import threading
import queue
import myEatThread
import time
class MyThread(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
self._thread_pool = []#线程池
self._queue = queue.Queue(10)#数据队列
self.init_thread_pool()#初始化线程池
self.run_pool_threads()#开启线程池中所有线程
#产生线程放入线程池
def init_thread_pool(self):
for i in range(10):
self._thread_pool.append(myEatThread.EatDataThread(self._queue))
#运行线程池中所有线程
def run_pool_threads(self):
for item in self._thread_pool:
item.setDaemon(True)
item.start()
def run(self):
while True:
#定时产生数据
for i in range(3):
eatData = myEatThread.EatData()
eatData.name = 'alex-' + str(i)
self._queue.put(eatData)
time.sleep(5)
if __name__ == '__main__':
myThread = MyThread()
myThread.setDaemon(True)
myThread.start()
while True:
time.sleep(100000)
消费者线程:主进程维护了多个消费者线程,消费者线程实时从队列中检测是否有需要处理的数据,如果有,则获取到进行处理
‘’‘myEatThread.py’‘’
import threading
import queue
class EatData():
def __init__(self):
self.name = ''
class EatDataThread(threading.Thread):
def __init__(self, dataQueue):
threading.Thread.__init__(self)
self.queue = dataQueue
def Eat(self, eatData):
strData = str(threading.currentThread()) + "--------------" + eatData.name + '\n'
print(strData)
def run(self):
while True:
eatData = self.queue.get(block = True, timeout = None)
self.Eat(eatData)