What I’m trying to do:
I have .csv with around 20 000 rows and I would like to read them and store them in the anvil database. If I use a normal server method to do this I get a server timeout, but if I put my code into a background task it seems to work way slower than with a normal method.
What I would like to do is either remove the server timeout for one of my methods, if that is possible, because I know that the method will be executed at some point and the user will know why it is slow.
Or find a way to execute a background task faster. For now, I have tested it with a couple of rows and it was working fine, but if I do it will a little more like 500 it just gets stuck forever.
Here is the code for this task.
For a long series of add_row
calls, consider wrapping them in a transaction.
Thank you for the advice, I tried it and it definitely helped to some extent, but I still get a timeout error at around 14%

The only way I know of to eliminate the TimeoutError is to use a Background Task. Even if it is slower than a regular call, at least it gets the job done!
Yes, but the problem there is that the background task does not seem to run at all.
Here is my code for testing the background task and as you see I have a task_state set in the first line of the task.
In my form, I have a timer that takes this state and just prints it, and if the state is not set it prints 0.

Here is the result after 2/3 minutes of running this task with a .csv file that contains only 2000 rows.
For some reason, it does not even run the first line. If I reduce the file size to a couple of rows 3/4, it works perfectly.
You will likely see a different result if you’re not rounding to the nearest whole percent.
Edit: try printing an unrounded value, and you may see the actual progress.
After a few days, I managed to find a new solution that fixed the problem.
What I did is split the function into two parts, one that is extracting the data frame from the file (a normal server callable) and a second function (a background task), which is just storing the rows in the database.
3 Likes