You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
How can I handle asynchronous logic?
For instance, how can I create a button 'sleep' which writes to a div 'sleeping' for a given amount of time, and when the process is finished, writes 'awake'?
I can create a callback launching the asynchronous process with an event on the button, and write 'sleeping' to the div, but how can I change to 'awake' when the process finishes?
Great question @thedimlebowski . Here's one way to do it. A few things to note:
1 - The request that runs the long process is blocking. You could modify this to use multiprocessing instead to run it asynchronously or something like celery
2 - When the process starts, it sets a semaphore (a "flag") by writing to the disk to prevent other processes from running. If multiple processes can run at the same time, then you can remove that logic.
3 - An interval poll continuously checks if the process is running or not and updates the RadioItems component with the status.
4 - Since one of the processes is blocking, we're running the code with multiple processes (processes=True) to allow multiple requests at the same time. In practice, just running the app with something like gunicorn in front of it will do this for you.
This example is well suited for using dash as the control panel for hardware, where only one user or session can have access to the system at a single time.
If your long running process is instead for just long-running data analysis code, I'd recommend:
Adding a caching layer to the function that runs the process so that subsequent requests are fast. Here's the tutorial on caching: https://plot.ly/dash/performance
Or, precomputing the results of the process and writing them to a file
Great. Thanks a lot for the detailed example.
I guess I wanted to avoid writing to the disk, but that might be the best option.
It did solve my issue, so many thanks!
@tantrev, you'd need to invoke a library that enables distributed task scheduling (such as Dask or Celery -- I know that Dask has good support for a range of distributed backends). So long as you manage the issue of the Dash callbacks being blocking, it's up to you how you go about scheduling distributed jobs kicked off by the callbacks.
Activity
chriddyp commentedon Jul 6, 2017
Great question @thedimlebowski . Here's one way to do it. A few things to note:
1 - The request that runs the long process is blocking. You could modify this to use multiprocessing instead to run it asynchronously or something like celery
2 - When the process starts, it sets a semaphore (a "flag") by writing to the disk to prevent other processes from running. If multiple processes can run at the same time, then you can remove that logic.
3 - An interval poll continuously checks if the process is running or not and updates the
RadioItemscomponent with the status.4 - Since one of the processes is blocking, we're running the code with multiple processes (
processes=True) to allow multiple requests at the same time. In practice, just running the app with something likegunicornin front of it will do this for you.This example is well suited for using dash as the control panel for hardware, where only one user or session can have access to the system at a single time.
If your long running process is instead for just long-running data analysis code, I'd recommend:
Let me know if that helps!
thedimlebowski commentedon Jul 7, 2017
Great. Thanks a lot for the detailed example.
I guess I wanted to avoid writing to the disk, but that might be the best option.
It did solve my issue, so many thanks!
tantrev commentedon Jul 19, 2018
Is there any way to farm out the async callbacks to a cluster of compute nodes?
tantrev commentedon Jul 19, 2018
Also, is there any way to utilize persistent python processes for the callbacks (similar to how RStudio Connect offers persistent R processes)?
ned2 commentedon Aug 2, 2018
@tantrev, you'd need to invoke a library that enables distributed task scheduling (such as Dask or Celery -- I know that Dask has good support for a range of distributed backends). So long as you manage the issue of the Dash callbacks being blocking, it's up to you how you go about scheduling distributed jobs kicked off by the callbacks.
chriddyp commentedon Aug 2, 2018
We've been using celery and redis recently with great results. See https://github.com/plotly/dash-redis-celery-periodic-updates/ for one example
cswarth commentedon Jan 28, 2021
The example above is out of date - here is an updated version of the same example that works with dash version 1.17.0
Merge pull request plotly#57 from plotly/update-circle
Merge pull request #57 from plotly/update-circle