Python3 ThreadPoolExecutor线程池 笔记

本文介绍了Python3中的ThreadPoolExecutor线程池,包括为何需要线程池、如何使用线程池、Future对象的使用,如as_completed、executor.map和wait的方法,以及ThreadPoolExecutor的源码分析,如初始化、提交任务的工作流程。
摘要由CSDN通过智能技术生成

Python3 ThreadPoolExecutor线程池

用到的模块:concurrent.futures

为什么需要线程池

主线程中可以获取某一个线程的状态或者某一个任务的状态,以及返回值。

futures可以让多线程和多进程编码接口一致

怎么使用

线程池的基类是concurrent.futures模块中的Executor,Executor提供了两个子类,即 ThreadPoolExecutor 和 ProcessPoolExecutor,其中 ThreadPoolExecutor 用于创建线程池,而 ProcessPoolExecutor 用于创建进程池。

如果使用线程池/进程池来管理并发编程,那么只要将相应的 task 函数提交给线程池/进程池,剩下的事情就由线程池/进程池来搞定。

Exectuor 提供了如下常用方法:

  • submit(fn, *args, **kwargs):将 fn 函数提交给线程池。*args 代表传给 fn 函数的参数,*kwargs 代表以关键字参数的形式为 fn 函数传入参数。
  • map(func, *iterables, timeout=None, chunksize=1):该函数类似于全局函数 map(func, *iterables),只是该函数将会启动多个线程,以异步方式立即对 iterables 执行 map 处理。
  • shutdown(wait=True):关闭线程池。

程序将 task 函数提交(submit)给线程池后,submit 方法会返回一个 Future 对象,Future 对象类主要用于获取线程任务函数的返回值。由于是异步任务,Python 用 Future 对象来作为 task 的返回容器。

Future 未来对象

Futrure 提供了以下方法:

class Future(object):
    """Represents the result of an asynchronous computation."""

    def __init__(self):
        """Initializes the future. Should not be called by clients."""
        self._condition = threading.Condition()	# 这里使用了threading中的条件变量 控制线程间同步
        self._state = PENDING	# 初始状态为待办
        self._result = None		# 运行结果
        self._exception = None	# 发生的异常
        self._waiters = []
        self._done_callbacks = []	# 回调函数集
  • cancel():取消该 Future 代表的线程任务。如果该任务正在执行或者已完成,不可取消,则该方法返回 False;否则,程序会取消该任务,并返回 True。

    def cancel(self):
        """Cancel the future if possible.
    
            Returns True if the future was cancelled, False otherwise. A future
            cannot be cancelled if it is running or has already completed.
        """
        with self._condition:
            if self._state in [RUNNING, FINISHED]:	#
                return False
    
            if self._state in [CANCELLED, CANCELLED_AND_NOTIFIED]:	#
                return True
    
            self._state = CANCELLED
            self._condition.notify_all()
    
            self._invoke_callbacks()
            return True
    
  • cancelled():返回 Future 代表的线程任务是否被成功取消。

    def cancelled(self):
        """Return True if the future was cancelled."""
        with self._condition:
            return self._state in [CANCELLED, CANCELLED_AND_NOTIFIED]
    
  • running():如果该 Future 代表的线程任务正在执行、不可被取消,该方法返回 True。

    def running(self):
        """Return True if the future is currently executing."""
        with self._condition:
            return self._state == RUNNING
    
  • done():如果该 Funture 代表的线程任务被成功取消或执行完成,则该方法返回 True。

    def done(self):
        """Return True of the future was cancelled or finished executing."""
        with self._condition:
            return self._state in [CANCELLED, CANCELLED_AND_NOTIFIED, FINISHED]
    
  • result(timeout=None):获取该 Future 代表的线程任务最后返回的结果。如果 Future 代表的线程任务还未完成,该方法将会阻塞当前线程,其中 timeout 参数指定最多阻塞多少秒。

    def result(self, timeout=None):
        """Return the result of the call that the future represents.
    
        Args:
        timeout: The number of seconds to wait for the result if the future
        isn't done. If None, then there is no limit on the wait time.
    
        Returns:
        The result of the call that the future represents.
    
        Raises:
        CancelledError: If the future was cancelled.
        TimeoutError: If the future didn't finish executing before the given
        timeout.
        Exception: If the call raised then that exception will be raised.
        """
        with self._condition:
            if self._state in [CANCELLED, CANCELLED_AND_NOTIFIED]:
                raise CancelledError()
                elif self._state == FINISHED:
                    return self.__get_result()
    
                self._condition.wait(timeout)	# 阻塞 获取结果
    
                if self._state in [CANCELLED, CANCELLED_AND_NOTIFIED]:
Python中的ThreadPoolExecutor线程池的一种实现方式,它提供了方便的接口来进行并发编程。在使用ThreadPoolExecutor时,通常会遇到异常捕获的问题。 当线程池中的线程执行任务时,如果任务发生异常,异常会被捕获,并通过Future对象返回给调用者。我们可以通过检查Future对象的状态来获取异常信息。Future对象是一个表示异步计算结果的对象,它可以用来检查任务是否完成、取消任务、获取任务的结果等。 在ThreadPoolExecutor中,可以通过submit方法来提交任务。这个方法返回一个Future对象,我们可以通过调用Future对象的result方法来等待任务完成并获取任务的结果。如果任务发生异常,result方法将会抛出异常,并将异常的类型和信息传递给调用者。 另外,我们还可以通过调用ThreadPoolExecutor的shutdown方法来关闭线程池。关闭线程池后,任何待处理的任务将会被取消,并且已提交但还未开始执行的任务将会被清除。我们可以通过调用Future对象的cancel方法来取消任务。 在代码中,我们可以使用try-except语句块来捕获线程任务中的异常。可以使用ThreadPoolExecutor的submit方法来提交任务,并通过返回的Future对象来获取任务的结果。在调用Future对象的result方法时,如果发生了异常,可以使用try-except语句块来捕获异常并处理异常。另外,在使用完线程池后,我们应该调用shutdown方法来关闭线程池,以释放资源。 总结起来,PythonThreadPoolExecutor提供了异常捕获机制,我们可以通过检查返回的Future对象来获取任务执行过程中的异常信息。在使用完线程池后,我们应该及时关闭线程池,以释放资源。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值