gman9732 gman9732 - 9 months ago 42
Python Question

Start IPython Parallel from within another Python script

Suppose I have two Python files

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)


and driver.py

from test import run

if __name__ == '__main__':
run()


I get the error [0:apply]: NameError: name 'b' is not defined.

This code will work if I call run() from within test.py, however, I do not want to do that. I want to call run() from within driver.py. Any ideas on how to fix this?

Answer Source

In test.py file import interactive, then, in map function use interactive(hi) instead of 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()