简体   繁体   English

将任务提交到Dask分布式集群时,本地python文件导入问题

[英]Local python files import issue when submitting a task to Dask distributed cluster

I have the following issue. 我有以下问题。 If my code is separated into different files, I cannot simply import and submit function to a cluster, because my local files are not present on workers machines. 如果将我的代码分成不同的文件,则不能简单地将函数导入并提交到集群,因为我的本地文件不存在于工作机上。 In order to tackle the problem, I need to manually merge files into one. 为了解决该问题,我需要手动将文件合并为一个。 It's can be done relatively simply for small examples but with a large project that is separated into several folders is very time-consuming. 对于较小的示例,它可以相对简单地完成,但是将大型项目分成多个文件夹非常耗时。 Example: 例:

localfile.py file: localfile.py文件:

def custom():
  print("Remote run")
  return

Now let's try to submit imported function to a cluster, cluster.py file: 现在,让我们尝试将导入的函数提交给集群cluster.py文件:

import localfile

x = client.submit(localfile.custom)

x.result()
# Import error here, details below

- --

# The protocol we write by default.  May be less than HIGHEST_PROTOCOL.
  File "/usr/local/lib/python3.6/site-packages/cloudpickle/cloudpickle.py", line 944, in subimport
ModuleNotFoundError: No module named 'localfile'

As you can see workers are not able to locate local python files so the task is failed. 如您所见,工作人员无法找到本地python文件,因此任务失败。

Is there is any way I can send my pickled code with imported local files? 有什么办法可以将导入的本地文件中的腌制代码发送出去? I understand that possible solution is just to upload local files to the workers, but when a code is modified very often and your cluster is dynamic is not an elegant solution. 我知道可能的解决方案只是将本地文件上传到工作程序,但是当代码经常被修改且集群是动态的时,这并不是一个很好的解决方案。 It would be great to be able to merge automatically your 'client.py' code with 'localfile.py', pickle it and send to the scheduler. 能够自动将“ client.py”代码与“ localfile.py”合并,将其腌制并发送到调度程序,将是很棒的。 Is it somehow possible? 有可能吗? Do you have any other ideas how to solve this problem? 您还有其他解决办法吗? Thank you! 谢谢!

As I think you will know, the client API provides upload_file , which can handle getting your module to workers into a place that it can be imported. 就像我想您会知道的那样,客户端API提供了upload_file ,可以处理将模块带给工作人员的位置,使其可以导入。

In the situation that workers come and go, you will not be able to ensure that a new worker has the file in the right place. 在工作人员来来往往的情况下,您将无法确保新工作人员将文件放置在正确的位置。 However, pickle, by its nature, refers objects/functions to source files, presumably in order to reduce the data size. 但是,pickle本质上会将对象/功能引用到源文件,大概是为了减小数据大小。

Two vague possibilities: 两种模糊的可能性:

  • you could perform compute operations specifically only on the same workers ( compute(workers=) ) that are live at the time of the upload, together with reload or other importlib hooks within the function that you run. 您可以仅在上载时处于活动状态的相同工作程序( compute(workers=) )上执行计算操作,还可以对运行的函数中的重载或其他importlib挂钩执行操作。
  • you could actually distribute function source as text, if you wished, and execute the definitions or, again, use importlib to make it into a module on the workers. 您实际上可以根据需要将函数源作为文本分发,并执行定义,或者再次使用importlib将其放入工作程序的模块中。

In either case, you would need to modify or wrap your function with something to deal with the extra step. 无论哪种情况,您都需要修改或包装一些函数来处理额外的步骤。

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

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