python 多线程 threading使用,自定义线程池实现生产者、消费者模式

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)
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值