pathos ImportError:找不到__import__

时间:2017-12-01 14:49:18

标签: python-3.5 pathos

我在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
&#13;
&#13;
&#13;

这是代码,它适用于线程池,也适用于列表理解

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)

1 个答案:

答案 0 :(得分:0)

原来我需要保存我的文件test.py,然后在函数定义之后立即打一个if name ==' main ',并且然后从命令行运行python test.py,现在它可以正常工作。

import pathos
import numpy as np
import time

def squared(x):
    # import time
    time.sleep(.5)
    return x ** 2


if __name__ == '__main__':
    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 = p.imap(squared, x)
    list(ans)
    et = time.time()
    print(et-st)

    st = time.time()
    ans = p.uimap(squared, x)
    list(ans)
    et = time.time()
    print(et-st)

    st = time.time()
    ans = t.map(squared, x)
    et = time.time()
    print(et-st)

    st = time.time()
    ans = t.imap(squared, x)
    list(ans)
    et = time.time()
    print(et-st)

    st = time.time()
    ans = t.uimap(squared, x)
    list(ans)
    et = time.time()
    print(et-st)