我需要将一个变量传递给dispy节点的setup()方法,这样我就可以告诉节点要从一个配置文件加载哪个数据集。否则,我必须为每个数据集编写一个特定的脚本,这将是痛苦的。
def setup(): # executed on each node before jobs are scheduled
# read data in file to global variable
global data
data = open('file.dat').read()
return 0
...
if __name__ == '__main__':
import dispy
cluster = dispy.JobCluster(compute, depends=['file.dat'], setup=setup, cleanup=cleanup)因此,我想传递字符串"file.dat"来设置,这样每个节点就可以实例化数据一次(因为它很大)。
发布于 2015-07-07 02:28:12
让我看看我是否理解这个问题。您希望将一个参数传递给安装程序,但是setup的实际调用发生在函数JobCluster的某个地方。这个调用不知道它应该传递一个参数。对吗?
解决方案是使用标准库functools.partial。你做这样的事:
if __name__ == '__main__':
import dispy
f = functools.partial(setup,"file.dat")
cluster = dispy.JobCluster(compute, depends=['file.dat'], setup=f, cleanup=cleanup)partial返回的对象,当没有参数调用时,用一个位置参数("file.dat")调用setup。您必须重写安装程序才能处理此参数,如下所示:
def setup(s): # executed on each node before jobs are scheduled
# read data in file to global variable
global data
data = open(s).read()
return 0https://stackoverflow.com/questions/31258188
复制相似问题