Access model in background task when complete

What I’m trying to do:

Hi all, I am new to Anvil and am struggling to come to understand how to retrieve a machine learning model that is being run in the background. ONce the model has been trained i then need to use to predict.

What I’ve tried and what’s not working:

I have tried using the joblib per this forum Storing Machine Learning Model in Anvil but it doesn’t work because a keras history object is being returned and cannot be used to predict (separate problem which I can fix if this issue is resolved). Additionally, the model I import from the data table per the before mentioned forum is the old model trained on old data. I require the latest model for the predictions, but for that it seems like I should be waiting for the background task to complete and waiting will result in a server time out I believe. I have no idea where to go from here.

If any more detail or information is required please let me know.
Code Sample:

# this is a formatted code snippet.
# paste your code between ``` 

Clone link:
share a copy of your app

The typical way to wait for a background task to complete is for the server function that starts the background task to return, then the form that called it uses a timer to periodically check the status of the background task.

1 Like

Thank you do you perhaps have an example that I can apply? based on my code looking something like this:

Code Sample:


### CLIENT CODE 
def build_model():
   model = Sequential()
# More model settings..
   return model

@anvil.server.backgorund_task
def train_model():
#implement fit and predict

### SERVER CODE
anvil.server.call(train_model)

# How to  use a timer to periodically check the status of the background task??

   

Have you gone through the background tasks tutorial? It shows a timer example: Running Tasks in the Background

1 Like