user2379888
user2379888

Reputation: 1195

Python pathos mulitprocessing "not defined" issue

I'm using the pathos for multiprocessing, and I'm trying to understand the following behavior. The following example works fine:

from pathos.multiprocessing import ProcessPool
import numpy as np


def f(x0, Dt, n_steps):
    x = x0
    for j in range(n_steps):
        x+=-Dt * x
    return x

Dt = 0.1
n_steps = 10
n_runs = 8

x0_vals = np.linspace(-1,1,n_runs)

g = lambda x0: f(x0, Dt, n_steps)

pool = ProcessPool(nodes=4)
results =pool.map(g, x0_vals)

print(results)

produces

[-0.3486784401, -0.24905602864285714, -0.1494336171857143, -0.049811205728571444, 0.04981120572857141, 0.1494336171857142, 0.24905602864285714, 0.3486784401]

Now, if, instead, I had placed the pool = ProcessPool(nodes=4) earlier in the code, like

from pathos.multiprocessing import ProcessPool
import numpy as np


def f(x0, Dt, n_steps):
    x = x0
    for j in range(n_steps):
        x+=-Dt * x
    return x

pool = ProcessPool(nodes=4)

Dt = 0.1
n_steps = 10
n_runs = 8

g = lambda x0: f(x0, Dt, n_steps)

x0_vals = np.linspace(-1,1,n_runs)
results =pool.map(g, x0_vals)
#pool.clear()

print(results)

then I get errors like:

NameError: name 'Dt' is not defined

I presume this is because the variable Dt is not defined on the workers in the pool. Is there a way to control that? Or should I just stick to only initializing the worker pool at the last possible moment?

Upvotes: 0

Views: 465

Answers (0)

Related Questions