gpt4 book ai didi

scala - 如何获取PySpark中的worker(executor)数量?

转载 作者:行者123 更新时间:2023-12-02 11:59:38 26 4
gpt4 key购买 nike

我需要使用这个参数,那么如何获取worker的数量呢?就像在 Scala 中一样,我可以调用 sc.getExecutorMemoryStatus 来获取可用的工作线程数量。但在 PySpark 中,似乎没有公开 API 来获取这个数字。

最佳答案

在 scala 中,getExecutorStorageStatusgetExecutorMemoryStatus 都返回包括驱动程序在内的执行器数量。就像下面的示例片段

/** Method that just returns the current active/registered executors
* excluding the driver.
* @param sc The spark context to retrieve registered executors.
* @return a list of executors each in the form of host:port.
*/
def currentActiveExecutors(sc: SparkContext): Seq[String] = {
val allExecutors = sc.getExecutorMemoryStatus.map(_._1)
val driverHost: String = sc.getConf.get("spark.driver.host")
allExecutors.filter(! _.split(":")(0).equals(driverHost)).toList
}

But In python api it was not implemented

@DanielDarabos answer也证实了这一点。

相当于Python中的这个...

sc.getConf().get("spark.executor.instances")

编辑(python):

%python
sc = spark._jsc.sc()
n_workers = len([executor.host() for executor in sc.statusTracker().getExecutorInfos() ]) -1

print(n_workers)

正如 Danny 在评论中提到的,如果您想交叉验证它们,可以使用以下语句。

%python

sc = spark._jsc.sc()

result1 = sc.getExecutorMemoryStatus().keys() # will print all the executors + driver available

result2 = len([executor.host() for executor in sc.statusTracker().getExecutorInfos() ]) -1

print(result1, end ='\n')
print(result2)

示例结果:

Set(10.172.249.9:46467)
0

关于scala - 如何获取PySpark中的worker(executor)数量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38660907/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com