Replacing flask internal web server with Apache
<p>I have written a single user application that currently works with Flask internal web server. It does not seem to be very robust and it crashes with all sorts of socket errors as soon as a page takes a long time to load and the user navigates elsewhere while waiting. So I thought to replace it with Apache. </p>

<p>The problem is, my current code is a single program that first launches about ten threads to do stuff, for example set up ssh tunnels to remote servers and zmq connections to communicate with a database located there. Finally it enters run() loop to start the internal server. </p>

<p>I followed all sorts of instructions and managed to get Apache service the initial page. However, everything goes wrong as I now don't have any worker threads available, nor any globally initialised classes, and none of my global variables holding  interfaces to communicate with these threads do not exist. </p>

<p>Obviously I am not a web developer. </p>

<p>How badly "wrong" my current code is?  Is there any way to make that work with Apache with a reasonable amount of work?  Can I have Apache just replace the run() part and have a running application, with which Apache communicates?  My current app in a very simplified form (without data processing threads) is something like this:</p>

<pre><code>comm=None
app = Flask(__name__)

class CommsHandler(object):
    __init__(self):
        *Init communication links to external servers and databases*
    def request_data(self, request):
        *Use initialised links to request something*
        return result

@app.route("/", methods=["GET"]):
def mainpage():
    return render_template("main.html")

@app.route("/foo", methods=["GET"]):
def foo():
    a=comm.request_data("xyzzy")
    return render_template("foo.html", data=a)

comm = CommsHandler()

app.run()
</code></pre>

<p>Or have I done this completely wrong?  Now when I remove app.run and just import app class to wsgi script, I do get a response from the main page as it does not need reference to global variable comm. </p>

<p>/foo does not work, as "comm" is an uninitialised variable.  And I can see why, of course. I just never thought this would need to be exported to Apache or any other web server. </p>

<p>So the question is, can I launch this application somehow in a rc script at boot, set up its communication links and everyhing, and have Apache/wsgi just call function of the running application instead of launching a new one? </p>

<p>Hannu</p>

<p>This is the simple app with flask run on internal server: </p>

<pre><code>from flask import Flask
app = Flask(__name__)

@app.route("/")
def hello():
    return "Hello World!"

if __name__ == "__main__":
    app.run()
</code></pre>

<p>To run it on apache server Check out <a href="http://flask.pocoo.org/docs/0.11/deploying/fastcgi/#creating-a-fcgi-file" rel="nofollow">fastCGI</a> doc :</p>

<pre><code>from flup.server.fcgi import WSGIServer
from yourapplication import app

if __name__ == '__main__':
    WSGIServer(app).run()
</code></pre>

python apache flask wsgi