Can I use functions imported from .py files in Dask/Distributed?

前端 未结 3 1902
-上瘾入骨i
-上瘾入骨i 2020-12-15 23:15

I have a question about serialization and imports.

  • should functions have their own imports? like I\'ve seen done with PySpark
  • Is the following just p
3条回答
  •  悲哀的现实
    2020-12-16 00:04

    To run an imported function on your cluster that is not available on the workers' environment, you can also create a local function from the imported function. This local function will then be pickled by cloudpickle. In Python 2 you can achieve this with new.function (see the new module). For Python 3 this could be achieved with the types module, but I haven't tried it.

    Your example above would then look like:

    In [3]: import mod
    
    In [4]: import new
    
    In [5]: def remote(func):
       ...:     return new.function(func.func_code, func.func_globals, closure=func.func_closure)
       ...:
    
    In [6]: e.run(remote(mod.hostname))
    Out[6]: {'tcp://10.0.2.15:44208': 'the hostname'}
    

提交回复
热议问题