2016-04-06 45 views
3

バックグラウンドでFlaskアプリケーションのインスタンスを起動して、Webdriverテストを実行できます。そのためには、&コマンドの出力をキャッチする必要があります。そのため、テストが終了したらプロセスを終了できます。バックグラウンドでプロセスを起動して出力を取得する

私はsubprocess.call()subprocess.check_output()を試しましたが、最初のプロセス番号をキャッチすることはできません。それ以外に何ができますか?

+0

[Flask-Testing](https://flask-testing.readthedocs.org/en/latest/#testing-with-liveserver)を見ましたか? – tzaman

+0

Uh。それを答えに変えれば、私はそれを受け入れるだろう。 – ruipacheco

+0

あなたのために働いてうれしい!完了しました。 – tzaman

答えて

1

あなたはFlask-Testing図書館、見てみたいことがありますあなたがそれに対してセレンテストを行うことができるようにフラスコサーバを実行するためのサポートを持っています。

1

あなたはpopenのでnohupを使用することができます

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") 

出力:

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は推奨されていませんか? – ruipacheco

+0

@ruipacheco、os.Popen、サブプロセスではありません.Popen、サブプロセスの99%がPopenを使用しています –

関連する問題