Answer the question
In order to leave comments, you need to log in
How to setup flask server?
Achtung! Help, I have a Flask server, the main task of which is to call a script in the system with the parameter passed in the request. Here is the code:
import subprocess
from flask import Flask
from flask import Response
app = Flask(__name__)
@app.route("/<url>", methods=['GET'])
def index(url):
cmd = './prg '
cmd = cmd + url
PIPE = subprocess.PIPE
p = subprocess.Popen(cmd, shell=True, stdin=PIPE, stdout=PIPE,
stderr=subprocess.STDOUT, close_fds=True, cwd='/home/prog')
resp = Response(p.stdout.read())
resp.headers['Access-Control-Allow-Origin'] = '*'
return resp
if __name__ == "__main__":
context = ('/var/www/httpd-cert/www-root/cert.crt', '/var/www/httpd-cert/www-root/cert.key')
app.run(host='0.0.0.0', port=9090, ssl_context=context)
Answer the question
In order to leave comments, you need to log in
You have done it wrong. You cannot block the execution thread in flask, since it is single-threaded, then you completely hang the server until the rendering of your task. For your task, you need to run a process in parallel, add the data of this process, for example, to a separate database or file, and immediately give the result with the number of your process.
To do this, you can use some simple task queue like python-rq.org
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question