什么是“权利"?方式关闭Dask LocalCluster? [英] What is the "right" way to close a Dask LocalCluster?

查看:148
本文介绍了什么是“权利"?方式关闭Dask LocalCluster?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用LocalCluster在笔记本电脑上使用dask-distributed,但是我仍然没有找到一种方法来关闭我的应用程序而不会引发一些警告或使用matplotlib触发一些奇怪的迭代(我正在使用tkAgg后端)

I am trying to use dask-distributed on my laptop using a LocalCluster, but I have still not found a way to let my application close without raising some warnings or triggering some strange iterations with matplotlib (I am using the tkAgg backend).

例如,如果我以此顺序关闭客户端和群集,则tk无法以适当的方式从内存中删除图像,并且出现以下错误:

For example, if I close both the client and the cluster in this order then tk can not remove in an appropriate way the image from the memory and I get the following error:

Traceback (most recent call last):
  File "/opt/Python-3.6.0/lib/python3.6/tkinter/__init__.py", line 3501, in __del__
    self.tk.call('image', 'delete', self.name)
RuntimeError: main thread is not in main loop

例如,以下代码生成此错误:

For example, the following code generates this error:

from time import sleep
import numpy as np
import matplotlib.pyplot as plt
from dask.distributed import Client, LocalCluster

if __name__ == '__main__':
    cluster = LocalCluster(
        n_workers=2,
        processes=True,
        threads_per_worker=1
    )
    client = Client(cluster)

    x = np.linspace(0, 1, 100)
    y = x * x
    plt.plot(x, y)

    print('Computation complete! Stopping workers...')
    client.close()
    sleep(1)
    cluster.close()

    print('Execution complete!')

sleep(1)行使该问题更有可能出现,因为它不会在每次执行时都发生.

The sleep(1) line makes the problem more likely to appear, as it does not occur at every execution.

我尝试停止执行的任何其他组合(避免关闭客户端,避免关闭群集,避免同时关闭两者)都会产生龙卷风问题.通常是以下

Any other combination that I tried to stop the execution (avoid to close the client, avoid to close the cluster, avoid to close both) generates problems with tornado, instead. Usually the following

tornado.application - ERROR - Exception in Future <Future cancelled> after timeout

什么是停止本地群集和客户端的正确组合?我想念什么吗?

What is the right combination to stop the local cluster and the client? Am I missing something?

这些是我正在使用的库:

These are the libraries that I am using:

  • python 3. [6,7] .0
  • 龙卷风5.1.1
  • 黄昏0.20.0
  • 分布式1.24.0
  • matplotlib 3.0.1

谢谢您的帮助!

推荐答案

根据我们的经验,最好的方法是使用上下文管理器,例如:

From our experience - the best way is to use a context manager, for example:

import numpy as np
import matplotlib.pyplot as plt
from dask.distributed import Client, LocalCluster 

if __name__ == '__main__':
    cluster = LocalCluster(
    n_workers=2,
    processes=True,
    threads_per_worker=1
    )
    with Client(cluster) as client:
        x = np.linspace(0, 1, 100)
        y = x * x
        plt.plot(x, y)
        print('Computation complete! Stopping workers...')

    print('Execution complete!')

这篇关于什么是“权利"?方式关闭Dask LocalCluster?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆