python并行空间

2024-06-26 13:54:52 发布

您现在位置:Python中文网/ 问答频道 /正文

我在一个简单的Python程序中遇到了一个名称空间问题:有人能给我指出正确的方向吗

import numpy as np
import simple_sim
from IPython.parallel import Client

prescale_steps = np.linspace(0.5, 1.5, 101)
val = []

c = Client()
dview = c[:]
dview.execute('import simple_sim')
dview.execute('from numpy import *')
dview['prescale_steps'] = prescale_steps
dview['val'] = val

detuning_steps = np.linspace(-11,11,101)


def fid(det):
    for p in prescale_steps:
        tlist, ret =    simple_sim.simple_simulation(pulse_file='/home/andreas/Dropbox/puls25p8gn15map.mat', pulse_length=0.5, gamma=0, detuning=det, prescale=p)
        val.append(np.array([d,p,ret[-1]]))
    return val



lview = c.load_balanced_view()
res = lview.map(fid, detuning_steps)
a = res.get()
a = np.asarray(a)

总是引发错误:global name 'simple_sim' is not defined,尽管它应该被定义,不是吗?你知道吗


Tags: fromimportnumpyclientexecutenpvalsim
2条回答

确保simple_sim位于ipython引擎的路径中,而不仅仅是ipython shell。你知道吗

也就是说,如果simple_sim.py~/mydir/中,您需要在~/mydir/中运行ipcluster start n=4,或者在$PYTHON_PATH中有~/mydir,以便shell运行ipcluster。你知道吗

不是100%确定,但可能是因为简单的sim卡不在网站包中或与dview不在同一文件夹中。换句话说,dview找不到简单的sim模块,因此会产生错误。然而,如果情况并非如此,我不确定是什么导致了这种错误。你知道吗

相关问题 更多 >