2012-08-10 14 views
6

test1.py:Subprocess.poll() restituisce erroneamente un valore

process = Popen(["python","test2.py"]) 
time.sleep(3) 

alive = process.poll() 
if alive is None: 
    print "Still running" 
else: 
    print "Not running\r\n" 
    print "%r" % alive 

test1.py uscita:

Not running
2

test2.py:

time.sleep(30) 
print "done" 

Cosa sta succedendo? Non dovrebbe questo ritorno "Ancora in esecuzione"?


A causa di un risultato contraddicendo Ecco il codice completo test1.py:

import cStringIO 
import os 
import cgi 
import time 
from subprocess import Popen 

def application(environ, start_response): 
    headers = [] 
    headers.append(('Content-Type', 'text/plain')) 
    write = start_response('200 OK', headers) 

    input = environ['wsgi.input'] 
    output = cStringIO.StringIO() 

    process = Popen(["python","test2.py"]) 
    time.sleep(3) 

    alive = process.poll() 
    if alive is None: 
     print >> output, "Still running" 
    else: 
     print >> output, "Not running\r\n" 
     print >> output, "%r" % alive 

    output.write(input.read(int(environ.get('CONTENT_LENGTH', '0')))) 
    return [output.getvalue()] 

Aggiornato test1.py:

process = Popen(["python","C:/wamp/www/python/popen/test2.py"], shell=True) 
    time.sleep(5) 

    alive = process.poll() 
    if alive is None: 
     #print >> output, "%r" % alive 
     print >> output, "Still running" 
    else: 
     print >> output, "Not running" 
     print >> output, "%r" % alive 
     print >> output, "Current working dir : %s" % os.getcwd() 
     print >> output, os.strerror(0) 

uscita Aggiornato:

Not running
0
Current working dir : C:\wamp\bin\apache\apache2.2.22
No error

+0

Per me funziona bene. Il mio output è 'Still running'. – RanRag

+0

da fuq ... beh allora ho un nuovo problema. Qualche idea? Pubblicherò altro codice ... BTW Ho pensato che fosse strano, perché non era dove nelle mie ricerche ho trovato un "2" come risultato per il sondaggio(). – Rawr

+0

Stesso risultato con RanRag, ho ottenuto "Ancora in esecuzione". –

risposta

7

Se Popen() non riesce a trovare test2.py, produce il "No such file or directory" Errore, con errno 2. Questo numero di errore viene restituito dal sondaggio(). Dal momento che ti sembra di essere in esecuzione questo script attraverso WSGI, qualcosa sembra essere gulping vostro stderr e non si vede il messaggio di errore:

$ cat test1.py 
from subprocess import Popen 
import time 

process = Popen(["python","doesnotexist.py"]) 
time.sleep(3) 

alive = process.poll() 
if alive is None: 
    print "Still running" 
else: 
    print "Not running\r\n" 
    print "%r" % alive 
$ python test1.py 
python: can't open file 'doesnotexist.py': [Errno 2] No such file or directory 
Not running 

2 

Ora, il problema è probabilmente perché il vostro attuale directory di lavoro dello script è non impostato sulla directory dello script dal server front-end, provare a stampare os.getcwd() per vedere se è ciò che si aspetta.

+0

Stavo per sottolineare che 'test2.py' non sembra importare il modulo' time', mentre quello potrebbe essere un errore, questo è la causa più immediata del tuo problema: – chepner

+1

+1, battimi a digitarlo.Se stai cercando di scoprire che cos'è un codice di uscita, puoi provare la funzione '' os.strerror() '' per vedere se dà un messaggio significativo: '' >> import os >> os.strerror (2) '' dà il messaggio '' 'No such file or directory'''. – Blair

+0

provato 'http: // localhost: 1234/python/popen/test2.py' e' C: \ wamp \ www \ python \ popen \ test2.py' stesso errore ... – Rawr

1

Secondo this

Uno stato di uscita di 2 indica un problema con la shell eseguire il comando. Hai provato a eseguire test2.py direttamente nella shell per verificare che non ci siano problemi con esso? Come Lie ha sottolineato potrebbe essere che la shell non riesce a trovare il file che stai cercando di eseguire, anche se potrebbe esserci un altro problema che lo ha causato.

Problemi correlati