I want to launch an instance of my Flask app in the background so I can run webdriver tests on it. For that I need to catch the output of the & command so I can kill the process when the test ends.
I've tried subprocess.call(), and subprocess.check_output() but I can't catch the process number of the first or background the process with the other. What else can I try?
asked Apr 6, 2016 at 19:20
ruipacheco
16.8k20 gold badges90 silver badges157 bronze badges
-
Have you looked at Flask-Testing ?tzaman– tzaman2016年04月06日 19:29:12 +00:00Commented Apr 6, 2016 at 19:29
-
Glad that worked for you! done.tzaman– tzaman2016年04月09日 20:06:51 +00:00Commented Apr 9, 2016 at 20:06
2 Answers 2
You may want to look at the Flask-Testing library, which has support for running your flask server so you can do selenium tests against it.
answered Apr 9, 2016 at 20:06
tzaman
48k11 gold badges93 silver badges118 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
You could use nohup with Popen:
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")
The output:
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
answered Apr 6, 2016 at 19:58
Padraic Cunningham
181k30 gold badges264 silver badges327 bronze badges
2 Comments
ruipacheco
Isn't Popen deprecated?
Padraic Cunningham
@ruipacheco, os.Popen, not subprocess.Popen, 99 percent of subprocess uses Popen
lang-py