【问题标题】:Start IPython Parallel from within another Python script从另一个 Python 脚本中启动 IPython Parallel
【发布时间】:2017-03-12 00:10:30
【问题描述】:

假设我有两个 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 中调用 run(),此代码将起作用,但是,我不想这样做。我想从 driver.py 中调用 run()。有关如何解决此问题的任何想法?

【问题讨论】:

标签: python ipython ipython-parallel


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-08-22
    • 1970-01-01
    • 1970-01-01
    • 2018-07-01
    • 1970-01-01
    • 2019-06-22
    • 1970-01-01
    相关资源
    最近更新 更多