1

I'm using wsgi apache and flask to run my application. I'm use from yourapplication import app as application to start my application. That works so far fine. The problem is, with every request a new instance of my application is created. That leads to the unfortunate situation that my flask application creates a new database connection but only closes it after about 15 min. Since my server allows only 16 open DB connections the server starts to block requests very soon. BTW: This is not happening when I run flask without apache/wsgi since it opens only one connection and serves all requests as I want.

What I want: I want to run only one flask instance which then servers all requests.

1 Answer 1

1

The WSGIApplicationGroup directive may be what you're looking for as long as you have the wsgi app running in daemon mode (otherwise I believe apache's default behavior is to use prefork which spins up a process to handle each individual request):

The WSGIApplicationGroup directive can be used to specify which application group a WSGI application or set of WSGI applications belongs to. All WSGI applications within the same application group will execute within the context of the same Python sub interpreter of the process handling the request.

You have to provide an argument to the directive that specifies a name for the application group. There's a few expanding variables: %{GLOBAL}, %{SERVER}, %{RESOURCE} and %{ENV:variable}; or you can specify your own explicit name. %{GLOBAL} is special in that it expands to the empty string, which has the following behavior:

The application group name will be set to the empty string. Any WSGI applications in the global application group will always be executed within the context of the first interpreter created by Python when it is initialised. Forcing a WSGI application to run within the first interpreter can be necessary when a third party C extension module for Python has used the simplified threading API for manipulation of the Python GIL and thus will not run correctly within any additional sub interpreters created by Python.

I would recommend specifying something other than %{GLOBAL}.

For every process you have mod_wsgi spawn, everything will be executed in the same environment. Then you can simply control the number of database connections based on the number of processes you want mod_wsgi to spawn.

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

4 Comments

I was already using WSGIApplicationGroup %{GLOBAL}. Seems like I had to use WSGIApplicationGroup %{RESOURCE}. Now it will only start one flask application and forwards the resource part directly to flask. if you could add those two points to your answer than I can accept it. Thanks for the help
Using WSGIApplicationGroup in anyway is likely wrong and especially setting it to %{RESOURCE} as that is actually the default so wouldn't make a difference. Your problem is going to be because of multiple processes, especially if running embedded mode. You should use daemon mode instead. code.google.com/p/modwsgi/wiki/ProcessesAndThreading blog.dscpl.com.au/2012/10/…
I kind of assumed it was already running in daemon mode. I'll add that to the answer.
I was running it for longer and the same problem appeared again. We could however find the really problem. It has nothing to do with flask. (sorry) We where using a Azure VM and connected it to a Azure DB. It looks like Microsoft has problems connection it's own tools/server. No we run our own DB in the VM. Everything works now fine.

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.