2016-06-08 16 views
0

我試圖使用python運行並行進程。我設法使用多處理模塊來做到這一點。在清除所有變量時在Python中進行並行處理

的問題是,當我在代碼的開頭添加一個「清除所有變量」,該算法使我這個錯誤:

Traceback (most recent call last):

File "", line 1, in runfile('C:/Users/cuca/Dropbox/USP/Pesquisa/PosQuali/Paralelo/Paraleltest2.py', wdir='C:/Users/cuca/Dropbox/USP/Pesquisa/PosQuali/Paralelo')

File "C:\Python27\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 685, in runfile execfile(filename, namespace)

File "C:\Python27\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 71, in execfile exec(compile(scripttext, filename, 'exec'), glob, loc)

File "C:/Users/cuca/Dropbox/USP/Pesquisa/PosQuali/Paralelo/Paraleltest2.py", line 36, in answer1 = result1.get(timeout=1)

File "C:\Python27\lib\multiprocessing\pool.py", line 563, in get raise TimeoutError

TimeoutError

當我從刪除「清除所有變量」程序算法的開始,它的工作原理。

任何想法爲什麼?

謝謝。

#Clear All 
all = [var for var in globals() if var[0] != "_"] 
for var in all: 
    del globals()[var] 


from multiprocessing import Pool 
import math 


def Func1(A): 
    for i in range(1000,100000): 
     R = A * math.cos(i) * math.cos(i) 
    return R 


if __name__ == '__main__': 

    pool = Pool() 
    result1 = pool.apply_async(Func1, [2]) 
    result2 = pool.apply_async(Func1, [4])  
    answer1 = result1.get(timeout=1) 
    answer2 = result2.get(timeout=1) 

回答

1

它可能與使用multiprocessing on Windows的限制有關。

Since Windows lacks os.fork() it has a few extra restrictions.

它可能是一些全局變量用於控制在Windows下的多處理,你打破了。嘗試打印變量名稱,然後刪除它們。

如果您描述爲什麼要清除所有全局變量,您將得到更有幫助的答案。我猜想你最好使用更少的全局變量,所以你不必清除它們。