2014-12-31 17 views
6

Ho sperimentato il multiprocessing e mi sono imbattuto in un mindblock con demoni.Perché un processo daemon di multiprocessing Python non stampa sullo standard output?

Ho un daemon e un processo non daemon, il daemon che emette l'output ogni secondo per un tempo indefinito, mentre il daemon non viene stampato immediatamente dopo l'avvio, rimane inattivo per 3 secondi, quindi stampa nuovamente e restituisce.

Il problema è che l'output previsto dal processo daemon non viene visualizzato.

Revisione delle domande SO precedenti sui daemon, i problemi comuni sembrano essere gli altri processi che terminano prima del demone, o lo stdout che richiede il flushing per mostrare l'output. Entrambi hanno (I penso) stato indirizzato, tuttavia continuo a vedere solo l'output stampato da processi non demonici.

Il codice:

from multiprocessing import Process, current_process 
import sys 
import time 

def worker(): 
    """ 
    Announce that the process has started, sleep 3 seconds 
    then announce that the process is ending. 
    """ 
    name = current_process().name 
    print name, 'starting...' 
    sys.stdout.flush() 

    time.sleep(3) 
    print name, 'ending...' 
    sys.stdout.flush() 

    return 


def daemon(): 
    """ 
    Announce that the process has started, beep, then beep 
    once every second 
    """ 
    name = current_process().name 
    print name, 'starting...' 
    print 'beep...' 
    sys.stdout.flush() 

    while True: 
     time.sleep(1) 
     print 'beep...' 
     sys.stdout.flush() 


if __name__=='__main__': 
    d = Process(target=daemon) 
    d.daemon = True 
    d.start() 

    p = Process(target=worker) 
    p.daemon = False 
    p.start() 

Output previsto:

Process-1 starting... # Order here may vary 
beep... 
Process-2 starting... 
beep... 
beep... 
Process-2 ending... #There may or may not be another beep here 

Che in realtà viene prodotto:

Process-2 starting... 
Process-2 ending... 

Qualunque consiglio sul perché questo sta accadendo sarebbe veramente apprezzato.

risposta

4

è possibile ottenere un quadro più chiaro l'ordine degli eventi per l'attivazione di registrazione mettendo

import multiprocessing as mp 
logger = mp.log_to_stderr(logging.INFO) 

dopo le altre istruzioni import. Poi il programma produrrà qualcosa come:

[INFO/Process-1] child process calling self.run() 
[INFO/MainProcess] process shutting down 
Process-1 starting... 
beep... 
[INFO/Process-2] child process calling self.run() 
[INFO/MainProcess] calling terminate() for daemon Process-1 
Process-2 starting... 
[INFO/MainProcess] calling join() for process Process-2 
Process-2 ending... 
[INFO/Process-2] process shutting down 
[INFO/Process-2] process exiting with exitcode 0 
[INFO/MainProcess] calling join() for process Process-1 

Così, i principali inizia a spegnere, poi termina Process-1, il processo demone. Ecco perché non vedi più alcun segnale acustico mentre Process-2 continua.

+0

Ah, vedo, il daemon viene terminato quando termina il processo principale, non quando tutti i processi figlio non demone terminano. Ha senso, molte grazie! Suppongo che l'aggiunta di un p.join() alla fine del processo principale faccia aspettare che il non-demone finisca. – zehnpaard

Problemi correlati