栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

Python ThreadPoolExecutor-是否保证回调函数在与提交的func相同的线程中运行?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Python ThreadPoolExecutor-是否保证回调函数在与提交的func相同的线程中运行?

本文档不保证在其中运行哪些线程回调。唯一记录的保证是,回调将在添加了回调的进程所属的线程中运行,但是可以是任何线程,因为您使用的是ThreadPoolExecutor而不是ProcessPoolExecutor:

添加的可调用对象按添加顺序被调用,并且始终在属于添加它们的进程的线程中调用。


在当前的ThreadPoolExecutor实现中,执行回调的线程取决于

Future
添加回调时的状态,以及是否
Future
取消。这些是实施细节;您不应该依赖它们,因为它们在不同的Python实现或不同版本中可能有所不同,并且如有更改,恕不另行通知。

如果在

Future
完成后添加回调,则该回调将在您调用的任何线程中执行
add_done_callback
。您可以通过查看
add_done_callback
源代码来看到此信息:

def add_done_callback(self, fn):    """Attaches a callable that will be called when the future finishes.    Args:        fn: A callable that will be called with this future as its only argument when the future completes or is cancelled. The callable will always be called by a thread in the same process in which it was added. If the future has already completed or been cancelled then the callable will be called immediately. These callables are called in the order that they were added.    """    with self._condition:        if self._state not in [CANCELLED, CANCELLED_AND_NOTIFIED, FINISHED]: self._done_callbacks.append(fn) return    fn(self)

如果状态

Future
指示已取消或已完成,
fn
则立即在当前执行线程中调用它。否则,它将添加到内部回调列表中,以在
Future
完成时运行。

例如:

>>> def func(*args):...  time.sleep(5)...  print("func {}".format(threading.current_thread()))>>> def cb(a): print("cb {}".format(threading.current_thread()))... >>> fut = ex.submit(func)>>> func <Thread(Thread-1, started daemon 140084551563008)>>>> fut = e.add_done_callback(cb)cb <_MainThread(MainThread, started 140084622018368)>

如果成功

cancel
调用取消了future ,则执行取消的线程会立即调用所有回调:

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

否则,执行将来任务的线程将调用回调。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/626066.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号