python实现线程池_python线程池的简单实现

为了在项目中管理优化线程数量,线程池是必不可少的。Python的在multiprocess中有进程池作为进程管理,但是Python的线程池却需要使用第三方模块进行支持。为了简单实现线程池,试做了以下线程池类。

from threading import lock, Thread, Event

from queue import Queue

import traceback

class thread_pool(object):

__active = False

def __init__(self, pool_size=3):

self.__event = Event()

self.__lock = lock()

self.__func_queue = Queue()

self.__thread_list = [Thread(target=self.handle_thread)

for i in range(pool_size)]

self.__inited = 0

self.__active = True

self.__start_threads_list()

def __start_threads_list(self):

self.__lock.acquire()

for thread in self.__thread_list[self.__inited:]:

thread.start()

self.__inited = len(threads_list)

self.__lock.release()

def join(self):

#将线程池所有线程join

self.__active = False

for t in self.__thread_list:

t.join()

def handle_thread(self):

handler = self.__func_queue.get(block=True)

while self.__active:

try:

target, args, kwargs = handler

target(*args, **kwargs)

handler = self.__func_queue.get(block=True)

except Exception as e:

traceback.print_exc()

def add_thead(target=func, args=(), kwargs={})

self.__func_queue.put((target, args, kwargs))

def expand_pool_size(self, pool_size=1):

self.__thread_list += [Thread(target=self.handle_thread)

for i in range(pool_size)]

self.__start_threads_list()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值