运行功能之前,dask是否等待资源可用?

时间:2019-05-17 21:44:53

标签: python dask dask-distributed

我正在使用一些计划在不久的将来在服务器上运行的代码。现在,它可以在我的本地计算机上运行,​​但是将有多个人同时运行该程序。我担心他们会使用更多的ram或vram。如果我使用dask,它将在执行函数调用之前等待可用资源吗?

示例代码

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from numba import njit
import numpy as np
from dask.distributed import Client, LocalCluster

@njit
def addingNumbers (big_array, big_array2, save_array):
    for i in range (big_array.shape[0]):
        for j in range (big_array.shape[1]):
            save_array[i][j] = big_array[i][j] * big_array2[i][j]

    return save_array


if __name__ == "__main__":
    cluster = LocalCluster()
    client = Client(cluster)


    big_array = np.random.random_sample((100, 3000))
    big_array2  = np.random.random_sample((100, 3000))
    save_array = np.zeros(shape=(100, 3000))


    x = client.submit(addingNumbers, big_array, big_array2, save_array)
    y = client.gather(x)

如果多个人同时运行上述代码,并且服务器几乎快要用完ram了,它将等待直到ram可用来提交函数,或者它将提交并且服务器将耗尽内存错误?

如果dask不等到ram可用,您将如何对函数调用进行排队? 谢谢

1 个答案:

答案 0 :(得分:2)

  

如果我使用dask,它将在执行函数调用之前等待可用资源吗?

Dask无法预测您的功能需要多少RAM。但是,您可以对存储的数据设置内存限制,如果Dask达到该限制,则一旦达到该限制,它将停止运行任务,而是将一些任务推入磁盘。参见https://distributed.dask.org/en/latest/worker.html#memory-management

  

您如何将函数调用排队?

最简单的解决方案是限制工作线程中活动线程的数量,或使用Worker resources来限制每个工作线程仅某些任务的并发性。