python - 从 dask 分布式线程池中分离任务

标签 python dask dask-distributed

我一直在阅读有关从工作线程上运行的任务生成新进程的文档。我从 here 发现了这个:

However, each running task takes up a single thread, and so if you launch many tasks that launch other tasks then it is possible to deadlock the system if you are not careful. You can call the secede function from within a task to have it remove itself from the dedicated thread pool into an administrative thread that does not take up a slot within the Dask worker

转移到管理线程意味着什么?所有插槽是否都具有相同的优先级或类似的优先级?管理线程确实有优先级吗?

作为一个具体的例子,这是我正在考虑尝试的:

from dask.distributed import get_client, secede

def compute_square(x):
    # Get locally created client
    client = get_client()
    secede()  # or not?
    if x > 5:
        client.submit(lambda x : x**2, x)

其中,compute_squarelambda 函数可能会运行多次,并且是一个计算量更大的函数,值得提交作业的 1ms 开销。 在这种情况下我应该使用 secede 吗?

最佳答案

What does it mean to move to an administrative thread?

Dask 工作线程维护一个固定大小的线程池来运行任务。当您调用 secede 时,您的计算将离开该线程池并打开另一个槽以供其他任务运行。您的任务线程仍然存在,但只是一个普通线程。术语“管理线程”除了“不在线程池中的线程”之外没有任何特殊含义。

Are all slots given the same priority or something similar?

一旦任务开始运行,它们之间就没有优先级了。它们都在普通的 Python 线程中运行,没有优先级。

And the administrative thread does have priority?

没有

Should I be using secede in this case?

如果出现以下情况,您应该调用 secede

  1. 您可能会调用足够多的任务,而这些任务都在等待其他任务,因此没有任务可以完成,因为所有线程池的线程都在等待其他插槽打开。
  2. 您不打算在该任务中做更多工作

如果出现以下情况,则不应调用 secede

  1. 在调用 secede 后,您打算在任务中执行更多计算密集型工作。这仍然没问题,但有点不礼貌,因为 Dask 会很舒服地启动其中几个任务,而没有任何限制。

关于python - 从 dask 分布式线程池中分离任务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45357277/

相关文章:

python - 在 2x2 网格中绘制形状图

dask - 我怎样才能在与提交它的机器不同的机器上获得 Dask 计算的结果?

python - Dask.distributed 每个节点仅使用一个核心

python - 求解两个变量的方程组 - Python

python - 使用python将excel文件中的数据导入SQL Server

python - Jupyter 实验室 - dask-labextension 不起作用

python - 使用 Lock 创建 Dask 延迟。错误: _thread._local没有execution_state

python - 启动时自动将数据集添加到 Dask 调度程序

python - 使用 xarray 在大 dask 数组上重新采样和分组 - 使用 map_blocks?

Python 数据操作 : How to group by ID, 然后子集行向前/向后 1 个月基于每个组中的日期时间条件?