我在 Linux 上运行以下代码 - 在虚拟环境中使用 Python 3.5 的 Ubuntu,并收到以下错误。它适用于线程和列表理解部分,但我在让多处理工作时遇到问题。
multiprocess.pool.RemoteTraceback:
"""
Traceback (most recent call last):
File "/usr/local/lib/python3.5/dist-packages/multiprocess/pool.py", line 119, in worker
result = (True, func(*args, **kwds))
File "/usr/local/lib/python3.5/dist-packages/multiprocess/pool.py", line 44, in mapstar
return list(map(*args))
File "/usr/local/lib/python3.5/dist-packages/pathos/helpers/mp_helper.py", line 15, in <lambda>
func = lambda args: f(*args)
File "<input>", line 2, in squared
ImportError: __import__ not found
"""
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "<input>", line 2, in <module>
File "/usr/local/lib/python3.5/dist-packages/pathos/multiprocessing.py", line 137, in map
return _pool.map(star(f), zip(*args)) # chunksize
File "/usr/local/lib/python3.5/dist-packages/multiprocess/pool.py", line 260, in map
return self._map_async(func, iterable, mapstar, chunksize).get()
File "/usr/local/lib/python3.5/dist-packages/multiprocess/pool.py", line 608, in get
raise self._value
ImportError: __import__ not found
这是代码,它适用于线程池,并且适用于列表理解
import pathos
import numpy as np
import time
def squared(x):
import time
time.sleep(.5)
return x ** 2
x = np.arange(400).reshape(50, 8)
p = pathos.pools.ProcessPool()
t = pathos.pools.ThreadPool()
st = time.time()
ans = [squared(i) for i in x]
et = time.time()
print(et-st)
st = time.time()
ans = p.map(squared, x)
et = time.time()
print(et-st)
st = time.time()
ans = t.uimap(squared, x)
list(ans)
et = time.time()
print(et-st)