简体   繁体   English

如何从普通函数中向 Python Dask 调度程序提交任务

[英]How to submit tasks to the Python Dask scheduler from within a plain function

I want to do something like this:我想做这样的事情:

client = Client()
def sub():
    client.submit(lambda: 'ok')

Calling sub() does not submit the task.调用sub()不会提交任务。 Calling client.submit(lambda: 'ok') directly does.直接调用client.submit(lambda: 'ok')就可以了。 I can't find anything in the docs discribing this behavior.我在描述这种行为的文档中找不到任何内容。 (I am on Dask version 1.1.4 and am currently not able to upgrade) (我使用的是 Dask 1.1.4 版,目前无法升级)

In your original code, the future generated by submit was not stored in a variable, and there was no reference to it after your function sub completed.在你的原始代码中, submit生成的 future 没有存储在变量中,并且在你的函数sub完成后没有引用它。 Therefore, Dask will have noticed that no client is any more interested in that future, and it will have been cleaned from the system - probably it had already run, and the memory of the result will have been released.因此,Dask 会注意到没有客户端对那个未来更感兴趣,并且它已经从系统中清除了 - 可能它已经运行了,并且结果的内存将被释放。

This is expected behaviour, and deciding in your code which futures have references to them is a fundamental concept in managing memory on the system, see https://distributed.dask.org/en/latest/memory.html#clearing-data这是预期的行为,在您的代码中决定哪些期货引用它们是管理系统内存的基本概念,请参阅https://distributed.dask.org/en/latest/memory.html#clearing-data

I suspect that when yo originally called submit while not in a function, a reference to the result was kept by your repl - if you had added a ;我怀疑当你最初在函数中调用submit时,你的 repl 保留了对结果的引用 - 如果你添加了; to the end of the line to suppress output, you would have seen the same behaviour as calling sub .到行尾以抑制输出,您会看到与调用sub相同的行为。

The task was submitted also when I executed sub() but I did not see this reflected in the Dask GUI after refreshing the GUI.当我执行sub()时也提交了任务,但在刷新 GUI 后我没有看到这反映在 Dask GUI 中。 You can check for certain that it works if you do如果你这样做,你可以检查它是否有效

def sub():
    return client.submit(lambda: 'ok')

sub().result()
# 'ok'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM