使用Dask分布式,函数如何自省以了解它是作为分布式未来还是“正常”函数运行?

问题描述

我正在使用dask distributed通过client.submit界面向我的集群提交函数。如果群集不可用,则认为串行,非分布式执行。我希望能够从该函数确定它是作为分布式未来还是作为正常函数运行。

例如,对于下面的work函数,我想知道它是通过client.submit(work)还是简单地以work()的形式运行。

def work():
    if is_distributed():
       # do stuff
    else:
       # do different stuff

is_distributed()应该是什么样?

解决方法

您应该使用函数library(tidytext) top_names %>% group_by(decade) %>% top_n(15) %>% ungroup %>% mutate(decade = as.factor(decade),name = reorder_within(name,n,decade)) %>% ggplot(aes(name,fill = decade)) + geom_col(show.legend = FALSE) + facet_wrap(~decade,scales = "free_y") + coord_flip() + scale_x_reordered() + scale_y_continuous(expand = c(0,0)) + labs(y = "Number of babies per decade",x = NULL,title = "What were the most common baby names in each decade?",subtitle = "Via US Social Security Administration") 。它将返回工作程序实例,或引发distributed.worker.get_worker。 所以你可以做类似的事情

ValueError