2017-12-01 9 views
0

저는 가상 환경에서 Python 3.5를 사용하는 우분투에서 다음 코드를 실행하고 있습니다. 스레딩과 목록 이해 부분에서 잘 작동하지만 멀티 프로세싱이 작동하는 데 문제가 있습니다. 여기 pathos ImportError : __import__을 찾지 못했습니다.

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) 

답변

0

내가 내 파일 test.py을 저장할 필요가 있다고 밝혀 스레드 풀로 작동하고, 지능형리스트를 들어, 코드 예를 들어, 이름이 == '메인'인 경우 : 함수 정의 직후에 명령을 실행 한 다음 명령 행에서 python test.py를 실행하면 a 지금 그것은 완벽하게 작동합니다.

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)