6

Ich führe die parallele Verarbeitung in Python unter Windows aus. Hier ist mein Code:python joblib Parallel unter Windows funktioniert nicht einmal "wenn __name__ == '__main__':" hinzugefügt wird

from joblib import Parallel, delayed 

def f(x): 
    return sqrt(x) 

if __name__ == '__main__': 
    a = Parallel(n_jobs=2)(delayed(f)(i) for i in range(10)) 

Hier ist die Fehlermeldung:

Process PoolWorker-2: 
Process PoolWorker-1: 
Traceback (most recent call last):  
File "C:\Users\yoyo__000.BIGBLACK\AppData\Local\Enthought\Canopy\App\appdata\canopy-1.5.4.3105.win-x86_64\lib\multiprocessing\process.py", line 258, in _bootstrap 
self.run() 
File "C:\Users\yoyo__000.BIGBLACK\AppData\Local\Enthought\Canopy\App\appdata\canopy-1.5.4.3105.win-x86_64\lib\multiprocessing\process.py", line 114, in run 
self._target(*self._args, **self._kwargs) 
File "C:\Users\yoyo__000.BIGBLACK\AppData\Local\Enthought\Canopy\App\appdata\canopy-1.5.4.3105.win-x86_64\lib\multiprocessing\pool.py", line 102, in worker 
task = get() 
File "C:\Users\yoyo__000.BIGBLACK\AppData\Local\Enthought\Canopy\User\lib\site-packages\joblib\pool.py", line 363, in get 
return recv() 
AttributeError: 'module' object has no attribute 'f' 
+0

Wahrscheinlich möchten Sie eine return-Anweisung 'def f (x): return sqrt (x)' hinzufügen. – jotrocken

+0

Nein, es ist das Gleiche. Ich lese auch irgendwo, dass Canopy die Dinge anders behandelt, also versuche ich 'sys.stdout.flush' nach' sqrt (x) 'hinzuzufügen, was auch nicht funktioniert – YKosinska

+0

Nein, ist es nicht. Der Rückgabewert ist 'None', wenn Sie' return' nicht angeben. – jotrocken

Antwort

7

zu this site das Problem laut ist Windows-spezifisch:

Ja: unter Linux wir gabeln, damit sie keine ist müssen die Funktion beizen, und es funktioniert gut. Unter Windows muss die Funktion pickleable sein, dh sie muss aus einer anderen Datei importiert werden. Dies ist eigentlich gute Praxis: Module zur Wiederverwendung zu machen.

Ich habe Ihren Code ausprobiert und es funktioniert einwandfrei unter Linux. Unter Windows läuft es OK, wenn es von einem Skript wie python script_with_your_code.py ausgeführt wird. Es schlägt jedoch fehl, wenn es in einer interaktiven Python-Sitzung ausgeführt wurde. Es funktionierte für mich, als ich die f Funktion in einem separaten Modul speicherte und es in meine interaktive Sitzung importierte.

NICHT ARBEITEN:

Interaktive Sitzung:

>>> from math import sqrt 
>>> from joblib import Parallel, delayed 

>>> def f(x): 
...  return sqrt(x) 

>>> if __name__ == '__main__': 
...  a = Parallel(n_jobs=2)(delayed(f)(i) for i in range(10)) 
... 
Process PoolWorker-1: 
Traceback (most recent call last): 
    File "C:\Python27\lib\multiprocessing\process.py", line 258, in _bootstrap 
    self.run() 
    File "C:\Python27\lib\multiprocessing\process.py", line 114, in run 
    self._target(*self._args, **self._kwargs) 
    File "C:\Python27\lib\multiprocessing\pool.py", line 102, in worker 
    task = get() 
    File "C:\Python27\lib\site-packages\joblib\pool.py", line 359, in get 
    return recv() 
AttributeError: 'module' object has no attribute 'f' 


ARBEITEN:
fun.py

from math import sqrt 

def f(x): 
    return sqrt(x) 

Interaktive Sitzung:

>>> from joblib import Parallel, delayed 
>>> from fun import f 

>>> if __name__ == '__main__': 
...  a = Parallel(n_jobs=2)(delayed(f)(i) for i in range(10)) 
... 
>>> a 
[0.0, 1.0, 1.4142135623730951, 1.7320508075688772, 2.0, 2.23606797749979, 2.449489742783178, 2.6457513110645907, 2.8284271247461903, 3.0] 
+0

jetzt funktioniert es! Danke!! – YKosinska

+1

@kchomski nette Post. Weißt du, warum wir __name__ == '__main__' in einer interaktiven Sitzung verwenden müssen? ist es nötig? – WillZ

Verwandte Themen