从另一个 Python 脚本中并行启动 IPython

Start IPython Parallel from within another Python script

假设我有两个 Python 个文件

test.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)

driver.py

from test import run

if __name__ == '__main__':
    run()

我收到错误 [0:apply]: NameError: name 'b' is not defined.

如果我从 test.py 中调用 运行(),此代码将起作用,但是,我不想那样做。我想从 driver.py 中调用 运行()。关于如何解决这个问题有什么想法吗?

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