0

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?

gman9732
  • 77
  • 2
  • 10
  • Possible duplicate of [Python name space issues with ipython parallel](http://stackoverflow.com/questions/10857250/python-name-space-issues-with-ipython-parallel) – Emin Mastizada Mar 12 '17 at 00:48

1 Answers1

0

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()
Emin Mastizada
  • 1,375
  • 2
  • 15
  • 30