2016-05-08 20 views
12

Sto provando a eseguire alcuni calcoli indipendenti (anche se la lettura dagli stessi dati). Il mio codice funziona quando l'eseguo su Ubuntu, ma non su Windows (Windows Server 2012 R2), dove ottengo l'errore:multiprocessing.Pool in jupyter notebook funziona su Linux ma non su Windows

'module' object has no attribute ...

quando provo ad usare multiprocessing.Pool (compare nella console del kernel, non come output nel notebook stesso)

(E ho già commesso l'errore di definire la funzione DOPO aver creato il pool, e l'ho anche corretto, non è questo il problema).

Questo accade anche sul più semplice degli esempi:

from multiprocessing import Pool 
def f(x): 
    return x**2 
pool = Pool(4) 
for res in pool.map(f,range(20)): 
    print res 

so che ha bisogno di essere in grado di importare il modulo (e non ho idea di come questo funziona quando si lavora nel notebook), e ho Ho sentito parlare di IPython.Parallel, ma non sono riuscito a trovare alcuna documentazione o esempi.

Qualsiasi soluzione/alternativa sarebbe molto gradita.

risposta

1

Vorrei postare questo come un commento poiché non ho una risposta completa, ma correggerò mentre capisco cosa sta succedendo.

from multiprocessing import Pool 

def f(x): 
    return x**2 

if __name__ == '__main__': 
    pool = Pool(4) 
    for res in pool.map(f,range(20)): 
     print(res) 

Questo funziona. Credo che la risposta a questa domanda sia here. In breve, i sottoprocessi non sanno di essere sottoprocessi e stanno tentando di eseguire ricorsivamente lo script principale.

Questo è l'errore mi viene data, che ci dà la stessa soluzione:

RuntimeError: 
     An attempt has been made to start a new process before the 
     current process has finished its bootstrapping phase. 

     This probably means that you are not using fork to start your 
     child processes and you have forgotten to use the proper idiom 
     in the main module: 

      if __name__ == '__main__': 
       freeze_support() 
       ... 

     The "freeze_support()" line can be omitted if the program 
     is not going to be frozen to produce an executable. 
+0

Giustissimo. In realtà i documenti spiegano diversi trucchi con il multiprocessing su Windows. Vedi https://docs.python.org/2/library/multiprocessing.html#windows – tdelaney