0

I have a simple flask application:

from flask import Flask

app = Flask(__name__)


@app.route('/')
def hello_world():
    text = '<h2>Text: </h2><form><input name="firstname" type="text">'
    select = '<h2>Select: </h2><select name="cars">' + \
              '<option value="volvo">Volvo</option>' + \
              '<option value="saab">Saab</option>' + \
              '<option value="fiat">Fiat</option> ' + \
              '<option value="audi">Audi</option>' + \
            '</select>'
    multiselect = '<h2>Multiselect: </h2><select name="mCars" multiple>' + \
              '<option value="volvo">Volvo</option>' + \
              '<option value="saab">Saab</option>' + \
              '<option value="fiat">Fiat</option> ' + \
              '<option value="audi">Audi</option>' + \
            '</select>'
    textarea = '<h2>Textarea: </h2><textarea name="message" rows="10" cols="30">' + \
                'The cat was playing in the garden.' + \
                '</textarea>'
    button = '<h2>Button</h2><button type="button" onclick="alert(\'Hello World!\')">Click Me!</button>'
    # datalist = '<h2>Datalist</h2><datalist id="browsers">' + \
    #               '<option value="Internet Explorer">' + \
    #               '<option value="Firefox">' + \
    #               '<option value="Chrome">' + \
    #               '<option value="Opera">' + \
    #               '<option value="Safari">' + \
    #             '</datalist> '
    return '<h1>Sample Form</h1>' + \
           text + \
           select + \
           multiselect + \
           textarea + \
           button


if __name__ == '__main__':
    app.run()

When I run the file itself it runs:

 * Serving Flask app "app" (lazy loading)
 * Environment: production
   WARNING: This is a development server. Do not use it in a production deployment.
   Use a production WSGI server instead.
 * Debug mode: off
 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)

I'm trying to run it from a runner file and continue execution of the file. It stops executing when flask runs. I've tried the following.

os.system('python ' + get_driver_file_path(Settings.TestProjectPath))

I think this makes sense because I am just running it.

I tried and it also runs but stops execution:

subprocess.call([Settings.PythonExecutablePath, get_driver_file_path(Settings.TestProjectPath)])

I tried:

server = Popen(get_driver_file_path(Settings.TestProjectPath), stdout=PIPE, stderr=PIPE, shell=True)

This does not seem to do anything.

How can I get it to run the flask server and continue execution?

4
  • How are you running this in production? Are you using gunicorn? Specifically, what is the command you would execute to run the app as blocking? Commented Jul 3, 2019 at 18:19
  • @RubenHelsloot There is no production yet. We are setting up a test framework and are using flask to get the gherkin test runner working. I'm just trying to run a script that will have flask app run followed by opening up some browsers with selenium and navigating to the page. Commented Jul 3, 2019 at 18:20
  • So you're probably running this with env variables likeFLASK_APP=api.py, FLASK_ENV=production, FLASK_DEBUG=0 and finally calling flask run, is that correct? Commented Jul 3, 2019 at 18:23
  • I am using the default env variables that flask comes with. In the script above it's called using app = Flask(__name__) then app.run() in the script file. Commented Jul 3, 2019 at 18:28

4 Answers 4

1

you just need to keep & to run application in background

import os
os.system("python "+ "app.py &" )
print "hello, its working, flask is running backgound "

for windows

import os
os.system("start /b python app.py")
print("It works..!")

make sure that python is added to PATH

Sign up to request clarification or add additional context in comments.

3 Comments

os.system('python ' + get_driver_file_path(Settings.TestProjectPath)+ " &")
I suppose it's not going to work on Windows. You may also have trouble if python is not on PATH for some reason.
solution should be platform agnostic
1

I ran subprocess.Popen(['python3', 'app.py']) successfully on my local machine, so this should technically work. The only thing I'm not sure of is that I ran this in a terminal, and closing the terminal might have closed the subprocess too.

1 Comment

@DavidTunnell the docs docs.python.org/3.7/library/… say that you shouldn't use PIPE or the child processes may block. What if you try without?
1

You do not want to run a Flask app and then continue your script.

Actually you want to start the Flask app, and continue your script while it is running.

I suppose multiprocessing "spawn" is what you need to start the Flask app.

You may need to wait until the new process (e.g. listening on localhost:5000) actually starts to serve requests if your script's later steps depend on it running.

Comments

0
from threading import Thread

if __name__ == '__main__':
    Thread(target=app.run).run()
    print("App is running")

Comments

Your Answer

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.