3

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
2
  • Have you looked at Flask-Testing ? Commented Apr 6, 2016 at 19:29
  • Glad that worked for you! done. Commented Apr 9, 2016 at 20:06

2 Answers 2

1

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
Sign up to request clarification or add additional context in comments.

Comments

1

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

2 Comments

Isn't Popen deprecated?
@ruipacheco, os.Popen, not subprocess.Popen, 99 percent of subprocess uses Popen

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.