从另一个Python脚本中启动IPython Parallel

时间:2017-03-12 00:10:30

标签: python ipython ipython-parallel

假设我有两个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()。关于如何解决这个问题的任何想法?

1 个答案:

答案 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()