Get updates of background tasks in client code

Hello,

I am wondering what is the best practice of taking some action in the main app code (client code) once some background task has finished executing. For example, if we make a background task that pulls data from an API, how can we set some kind of trigger in our client code to know that it has been completed, and now do some new action?

I know we can use methods like is_completed() to check on the status of it, so should we simple run a while loop until we get True from that method? But then we block the rest of the application. So is there some better way to get or check for updates periodically without blocking the code?

Thanks

Most people use a timer component, it will not block the rest of the page, other than while it is executing.

Also, unless you are expecting the return value of a task to be None , from past experience it is more reliable to check the return result of the task instead of .is_completed()

So

if task_object.get_return_value() is not None:
    ...  # code that does something regarding the background task being complete
3 Likes

Here is a link to an example of going overboard with background tasks and timer components, but it does show them off in an over-the-top way, with a clone link to the code:

1 Like

Thanks guys! I got mine working with timers for getting updates from the background tasks and it works great.

1 Like