Dash callbacks are not async - handling multiple requests and callbacks in parallel

My understanding is that gunicorn defaults to 1 worker process, so just using it out of the box as you indicate won’t allow your app to handle multiple callbacks in parallel. To specify multiple workers just do:

$ gunicorn -w 2 app:server

In my experience this will break the CSRF protection that Dash automatically adds to Flask, as the different workers are initialised with different tokens. For this to work, you’ll need to either explicitly supply your own CSRF token or as a simple/hacky workaround you can specify that gunicorn forks the Python process after the app is loaded thereby ensuring the workers share the same CSRF token:

$ gunicorn -w 2 --preload app:server

This Dash Issue contains a discussion of all that: https://github.com/plotly/dash/issues/132

Depending in what your callbacks are doing, using asynchronous worker(s) with multiple threads might be useful. There are different types of async workers, here’s how you can use the gevent one:

$ pip install gunicorn gevent
$ gunicorn --worker-class gevent --threads 8

You can also combine to have multiple async worker processes.

3 Likes