假设我有两个Python文件
test.py
loadTiles
和 driver.py
from ipyparallel import Client
def hi(a):
return b + (a * 2)
def run():
b = 3
client = Client()
view = client[:]
view.push({'b':b})
results = view.map(hi, [0,1,2,3,4])
for r in results:
print(r)
我收到错误 [0:apply]:NameError:名称'b'未定义。
如果我从test.py中调用run(),这段代码就可以工作,但是,我不想这样做。我想从driver.py中调用run()。关于如何解决这个问题的任何想法?
答案 0 :(得分:0)
在test.py
文件导入interactive
中,然后在map
函数中使用interactive(hi)
代替hi
:
from ipyparallel import Client
from ipyparallel.util import interactive
def hi(a):
global b
return b + (a * 2)
def run():
b = 3
client = Client()
view = client[:]
view.push({'b':b})
results = view.map(interactive(hi), [0,1,2,3,4])
for r in results:
print(r)
if __name__ == '__main__':
run()