Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 834
Arek S Avatar asked Nov 06 '25 06:11

Arek S


1 Answers

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
like image 88
pech0rin Avatar answered Nov 08 '25 22:11

pech0rin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!