Long Functions with anvil.server.TimeoutError: Server code took too long

What I’m trying to do:
I’m running a long function that calls other functions and it hits the timeout error. Is there a way around this? the process is broken up into many functions but when I put them all together into one, i can’t continue.

Should I just call an python server on a vps to do all the long processing and send results back to anvil via uplink?

If you are on any plan other than the free plan, you can use a background task to run your long function.

When you run the task, you can return to the client a task object that will be connected to the server.

By polling this task object periodically (such as with a timer) like:

example_task_object.get_return_value()

the result of this will be None, until the task is completed. It will then become the result of the return of the function decorated as a background task in the server module.

1 Like

ok thanks, i had plan to do background task, i guess i’ll just have to test out the code in background task.

Here is a link to a post where I aggregated a bunch of other links to posts about background tasks.

I am sure there are newer ones since I wrote it as well.

1 Like

So I followed this link just to run a background task, and kick it off by calling it in the __init__ of my form. Seems to run faster than not calling it as a background task. I didn’t schedule it in the task scheduler yet but will. Would it make sense to set most or all of server calls as a background task and a one last one liner function as a anvil.server.callable just for the forms to call it?

Something like that would be a way to asynchronously load the data for your page, if you choose to structure it that way.

You could have one anvil.server.call('launch_all_tasks') function that could return a group of task objects that could be launched from inside the server module and returned.

You can also create timer objects directly from code on-the-fly without adding them to the GUI part of the IDE. They can even be written to delete themselves once they are done loading the results of your background tasks.

Here is an example of this behavior taken to the extreme (with a clone example)