Search code examples
flaskgunicornpython-multithreadingapache-zookeeperkazoo

Setting a watch function using kazoo when calling Zookeeper from a Flask application deployed using gunicorn


I have a Flask application deployed using gunicorn. I'd like to integrate the application with a Zookeeper ensemble using kazoo, as the app relies on having up to date data from a get_children() call on one of the Znodes.

One option is for me to have the application fetch the data every time before it uses the data. The data don't change that often, so this is inefficient. Instead of polling Zookeeper, I'd like to call get_children() with a watch function when the app starts up, to have that watch function called back when there's a change in the data.

My question is, where do I set up this watch function effectively to ensure it will be called while the app is running? Should I set it up in the main file of the application (the file that's passed to the gunicorn command)? I guess I'm confused as to which thread will run the watch function while the app is running within gunicorn.


Solution

  • I found the answer to this upon further research. The kazoo library allows for passing a handler object when creating a KazooClient object. The default handler is SequentialThreadingHandler, which will run watch functions in a thread that kazoo creates separately from the main thread.

    The SequentialThreadingHandler works in my Flask + gunicorn setting. Each gunicorn worker process will create a separate thread for watch functions when this handler is used.