I want to fire up a flask server from within a file, but after the server is started I want to return control to the function which has actually started the server, so that function can continue what it is doing.

I have tried using subprocess.Popen() to run server from a separate file. I try not to return an object and just start a server in a separate process, but it always starts the server and doesn't yield control.

this is server.py:

from flask import Flask
import requests
RestServer = Flask(__name__)
class ControlPeerRestInterface:

    def __init__(self):
        pass

    @RestServer.route('/cp/v1/status')
    def server_status():
        return ("***************")


RestServer.run(port=8700)

this is serverRunner.py, which should start the server and then continue whatever it wants to do:

import subprocess
import sys

def start_server():

    br_command = [
                sys.executable,
                'server.py'
            ]
    br_process = subprocess.Popen(br_command)
    print (br_process.pid)



start_server()

1) Start a flask server from start_server.py 2) the flask server should be used as a REST API. 3) the serverRunner.py should continue doing whatever it wants to do after it has fired up the server and is communicating with that server. 4) serverRunner.py will close the flask server after it is done with whatever it wants to do after starting the server.

0 Answers