M
M
Mors Superberg2017-12-15 13:48:36
Flask
Mors Superberg, 2017-12-15 13:48:36

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)


I ran into a problem, my server crashes after accessing it, what could be the reason for this? How to set up this process optimally? It is worth noting that the program that is called is computationally expensive and works quite a lot, while using all the available cores. (sometimes up to several minutes)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Cheremisin, 2017-12-15
@NooooN

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 question

Ask a Question

731 491 924 answers to any question