Python 3.x nameerror,Lambda中未定义函数
运行Python 3.x nameerror,Lambda中未定义函数,python-3.x,lambda,jupyter-notebook,python-multiprocessing,multiprocess,Python 3.x,Lambda,Jupyter Notebook,Python Multiprocessing,Multiprocess,运行t\u test\u sequential=timeit(test\u sequential,number=1)工作正常,因此run\u test函数应该没有任何问题,但运行t\u test\u parallel=timeit(test\u parallel,number=1)会导致以下错误 from testfuntions import run_test from multiprocess import Pool from timeit import timeit def te
t\u test\u sequential=timeit(test\u sequential,number=1)
工作正常,因此run\u test
函数应该没有任何问题,但运行t\u test\u parallel=timeit(test\u parallel,number=1)
会导致以下错误
from testfuntions import run_test
from multiprocess import Pool
from timeit import timeit
def test_sequential():
for _ in range(nruns):
_ = run_test(*inputs)
def test_parallel():
with Pool(ncores) as p:
_ = p.map(lambda _: run_test(*inputs), range(nruns))
t_test_sequential = timeit(test_sequential, number=1)
t_test_parallel = timeit(test_parallel, number=1)
RemoteTraceback回溯(最近一次调用)
远程回溯:
"""
回溯(最近一次呼叫最后一次):
worker中的文件“C:\Users\Anaconda3\envs\rma\lib\site packages\multiprocess\pool.py”,第125行
结果=(True,func(*args,**kwds))
文件“C:\Users\Anaconda3\envs\rma\lib\site packages\multiprocess\pool.py”,第48行,在mapstar中
返回列表(映射(*args))
文件“”,第4行,在
NameError:未定义名称“运行测试”
上述异常是以下异常的直接原因:
NameError回溯(最近一次呼叫上次)
在里面
1个打印(f“并行{nruns}次运行测试”)
---->2 t_测试_并行=时间IT(测试_并行,数字=1)
timeit中的~\Anaconda3\envs\test\lib\timeit.py(stmt、setup、timer、number、globals)
231编号=默认编号,全局编号=无):
232“创建计时器对象和调用timeit方法的便利函数。”“”
-->233返回计时器(stmt、设置、计时器、全局)。timeit(数字)
234
235 def重复(stmt=“通过”,setup=“通过”,计时器=默认计时器,
timeit中的~\Anaconda3\envs\test\lib\timeit.py(self,number)
175 gc.disable()
176尝试:
-->177定时=自内(it、自定时器)
178最后:
179如果是gcold:
内部(\u it,\u timer,\u stmt)中的~\Anaconda3\envs\test\lib\timeit.py
在test_parallel()中
2 def测试_并行()
3,池(ncores)为p:
---->4=p.map(lambda:运行测试(*输入),范围(nruns))
映射中的~\Anaconda3\envs\test\lib\site packages\multiprocess\pool.py(self、func、iterable、chunksize)
362在返回的列表中。
363 '''
-->364返回self.\u map\u async(func、iterable、mapstar、chunksize).get()
365
366 def statestp(self、func、iterable、chunksize=None):
get中的~\Anaconda3\envs\test\lib\site packages\multiprocess\pool.py(self,超时)
769返回自身值
770其他:
-->771提高自我价值
772
773 def_装置(自、i、obj):
NameError:未定义名称“运行测试”
第一行看起来很奇怪。它应该是来自的,而不是来自
的。另外,模块名为testfunctions
或testfunctions
?@timgeb,感谢您识别原始代码中的输入错误。我在这里更正了它。模块名为testfunctions请提供一个。有几件事错误,例如,输入
和NRUN
未定义。因为多处理
可能有一些锐边,具体取决于调用方式(例如,需要主保护),非常重要的是我们有一些有效的代码,而不必猜测缺少哪些部分。@Mistermiagi,由于此代码段是从自定义程序中提取的,因此创建一个示例可能有点困难。您能否详细说明您对“需要主警卫”的评论“我试图在Jupternotebook上测试一些实现,但没有将main作为普通python程序使用,这在调用多进程时会有问题吗?谢谢。
RemoteTraceback Traceback (most recent call last)
RemoteTraceback:
"""
Traceback (most recent call last):
File "C:\Users\Anaconda3\envs\rma\lib\site-packages\multiprocess\pool.py", line 125, in worker
result = (True, func(*args, **kwds))
File "C:\Users\Anaconda3\envs\rma\lib\site-packages\multiprocess\pool.py", line 48, in mapstar
return list(map(*args))
File "<ipython-input-7-a0da8e60c15a>", line 4, in <lambda>
NameError: name 'run_test' is not defined
The above exception was the direct cause of the following exception:
NameError Traceback (most recent call last)
<ipython-input-9-457d6ed45feb> in <module>
1 print(f"Run TEST in parallel {nruns} times.")
----> 2 t_test_parallel = timeit(test_parallel, number=1)
~\Anaconda3\envs\test\lib\timeit.py in timeit(stmt, setup, timer, number, globals)
231 number=default_number, globals=None):
232 """Convenience function to create Timer object and call timeit method."""
--> 233 return Timer(stmt, setup, timer, globals).timeit(number)
234
235 def repeat(stmt="pass", setup="pass", timer=default_timer,
~\Anaconda3\envs\test\lib\timeit.py in timeit(self, number)
175 gc.disable()
176 try:
--> 177 timing = self.inner(it, self.timer)
178 finally:
179 if gcold:
~\Anaconda3\envs\test\lib\timeit.py in inner(_it, _timer, _stmt)
<ipython-input-7-a0da8e60c15a> in test_parallel()
2 def test_parallel():
3 with Pool(ncores) as p:
----> 4 _ = p.map(lambda _: run_test(*inputs), range(nruns))
~\Anaconda3\envs\test\lib\site-packages\multiprocess\pool.py in map(self, func, iterable, chunksize)
362 in a list that is returned.
363 '''
--> 364 return self._map_async(func, iterable, mapstar, chunksize).get()
365
366 def statestp(self, func, iterable, chunksize=None):
~\Anaconda3\envs\test\lib\site-packages\multiprocess\pool.py in get(self, timeout)
769 return self._value
770 else:
--> 771 raise self._value
772
773 def _set(self, i, obj):
NameError: name 'run_test' is not defined