我想在后台启动Flask应用程序的实例,以便我可以对它运行webdriver测试。为此,我需要捕获&
命令的输出,这样我就可以在测试结束时终止进程。
我已经尝试了subprocess.call()
和subprocess.check_output()
,但我无法捕捉第一个进程号或后者与另一个进程的后台进程。我还能尝试什么?
答案 0 :(得分: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
答案 1 :(得分:1)
您可能需要查看Flask-Testing库,该库支持运行您的烧瓶服务器,以便您可以对其进行硒测试。