Arek S Arek S -4 years ago 204
Python Question

Python + uwsgi - multiprocessing and shared app state

We have a flask app running behind uwsgi with 4 processes. Its an API which serves data from one of our two ElasticSearch clusters.

On app bootstrap each process pulls config from external DB to check which ES cluster is active and connects to it.

Evey now and then POST request comes (from aws SNS service) which informs all the clients to switch ES cluster. That triggers the same function as on bootstrap - pull config from DB reconnect to active ES cluster.

It works well running as a single process, but when we have more then one process running only one of them will get updated (the one which picks up POST request)... where other processes are still connected to inactive cluster.

Pulling config on each request to make sure that ES cluster we use is active would be to slow. Im thinking to install redis locally and store the active_es_cluster there... any other ideas?

Answer Source

I think there are two routes you could go down.

  1. Have an endpoint "/set_es_cluster" that gets hit by your SNS POST request. This endpoint then sets the key "active_es_cluster", which is read on every ES request by your other processes. The downside of this is that on each ES request you need to do a redis lookup first.

  2. Have a seperate process that gets the POST request specifically (I assume the clusters are not changing often). The purpose of this process is to receive the post request and just have uWSGI gracefully restart your other flask processes.

The advantages of the second option:

  • Don't have to hit redis on every request
  • Let uWSGI handle the restarts for you (which it does well)
  • You already setup the config pulling at runtime anyway so it should "just work" with your existing application
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download