2016-04-06 13 views
3

Arka planda bir Flask uygulamasının bir örneğini başlatmak istiyorum, böylece web tarayıcı testlerini çalıştırabilirim. Bunun için & komutunun çıkışını yakalamam gerekiyor, böylece test bittiğinde işlemi öldürebilirim.Arka planda işlemi başlatın ve çıktıyı alın

subprocess.call() ve subprocess.check_output()'u denedim, ancak işlemin ilkini yakalayamıyorum veya işlemin diğeriyle arka planda kalmasını sağlayamıyorum. Başka ne deneyebilirim?

+0

[Flask-Testing] 'a (https://flask-testing.readthedocs.org/en/latest/#testing-with-liveserver) baktınız mı? – tzaman

+0

Uh. Onu bir cevap haline getirin ve kabul edeceğim. – ruipacheco

+0

Sizin için çalıştığıma sevindim! yapılır. – tzaman

cevap

1

Sen Flask-Testing kütüphane, bakmak isteyebilirsiniz hangi Flask sunucunuzu çalıştırmak için destek sağlar, böylece selenyum testleri yapabilirsiniz.

1

Sen POPEN ile nohup kullanabilirsiniz:

from subprocess import Popen, check_call 

from os import devnull 

p = Popen(["nohup", "python", "test.py"], stdout=open(devnull, "w")) 

import time 

print(p.pid) 
for i in range(3): 
    print("In for") 
    time.sleep(1) 

check_call("ps -ef | grep {} | grep -v grep".format(p.pid), shell=True) 
p.terminate() 
check_call("ps -ef | grep {} | grep -v grep".format(p.pid), shell=True) 

test.py:

import time 
while True: 
    time.sleep(1) 
    print("Still alive") 

çıkışı:

In [3]: from os import devnull 

In [4]: p = Popen(["nohup", "python", "b.py"], stdout=open(devnull, "w")) 
nohup: ignoring input and redirecting stderr to stdout 
In [5]: print(p.pid) 
28332 

In [6]: for i in range(3): 
    ...:   print("In for") 
    ...:   time.sleep(1) 
    ...:  
In for 
In for 
In for 

In [7]: check_call("ps -ef | grep {} | grep -v grep".format(p.pid), shell=True) 
padraic 28332 28301 1 20:55 pts/8 00:00:00 python test.py 
Out[7]: 0 

In [8]: p.terminate() 

In [9]: check_call("ps -ef | grep {} | grep -v grep".format(p.pid), shell=True) 
padraic 28332 28301 0 20:55 pts/8 00:00:00 [python] <defunct> 
Out[9]: 0 
+0

Popen onaylanmadı mı? – ruipacheco

+0

@ruipacheco, os.Popen, altprocess.Popen, alt işlemlerin yüzde 99'u Popen kullanır –

İlgili konular