Aaron
April 18, 2022, 5:17pm
1
I have a report which takes about 40 sec to generate (there are a lot graphs)
Now I used Brookes tutorial on PDF to generate a pdf and got that error message:
anvil.server.TimeoutError: Server code took too long
What shall I do? (don´t want to seperate the report on several forms).
Cheer Aaron
Background tasks are the usual answer for off-loading long running tasks: Anvil Docs | Background Tasks
That only works if you’re on a paid plan, though. I’m pretty sure that for the free plan background tasks have the same time limit as server functions.
1 Like
Aaron
April 18, 2022, 5:40pm
3
Thx - that is a brillant idea (I am on a paid plan).
Aaron
April 18, 2022, 5:58pm
4
Background tasks is running,
on client side I want to save the pdf with
anvil.media.download(pdf)
Then I get the error:
AttributeError: ‘LiveObjectProxy’ object has no attribute ‘url’
What can I do to save the pdf?
There is no question in your post and not even enough detail to guess at what it might be.
(I suppose I could guess, but that’s not a great use of anyone’s time).
Please read:
Welcome to the Q&A Forum!
Help each other by asking questions about Anvil, and answering other people’s.
Post each question as a new topic - and if someone helps you, don’t forget to say thank-you. Try to provide information to help people answer your questions:
Be specific about what you’re trying to do.
Be specific about what your problem is (eg what’s not working), and what you have already tried.
Include code samples if you can.
It often helps if you can share your app’s source code and …
And then have another go.
ianb
April 18, 2022, 8:34pm
6
Here is a link where someone asked a similar question and I pointed them to a clone link with some example code of background tasks working:
Like @james2 said, background tasks are probably the answer you want to go with to do anything that is taking longer than 30 seconds.
Here is a link to a post with a clone link to an example app I made: *
*wow than sentence seems way more complicated than it actually is.
Here is another link where I just showed off an easy formatting style to keep track of what is what:
I have all of my background tasks separated out and get called by other functions that are organized as ‘background task generators’, this way I can grab information about the task object before I return it to the client, or another function etc.
This way you could grab the id from the task object, store it in your data table, then return the task object to whatever called it to begin with (or send it nowhere, whatever).
Example server module:
[image]
blah blah background task decorated fun…
Finally, here is a link to when I discovered background tasks due to someone asking about them, with some of the sharper points of the docs smoothed out for a newcomer to the concept:
Let me start by saying I have not used background tasks, so I read the documentation and I think you just need to layer one more step by creating a callable function that returns a task object that is created by running your decorated ‘@anvil.server.background_task’ function. The result of this background task function should return the media object you want.
When task.is_completed() becomes True, the value of task.get_return_value() will be the media object.
This should solve many issues someone might have, if the issue really is background task-related.
Edit: You may also run into a problem ( as detailed in the last link ) where you are trying to return a media object directly to the client from a background task, AFAIK that is still not something you are able to do, and something there are have been many discussions about already, with workarounds, etc.
Aaron
April 19, 2022, 11:18am
7
Thank you Ian,
I will try that.
Aaron
ianb
April 19, 2022, 2:23pm
8
I modified the title of your post to add (too slow) that way when someone else searches the forum for something like:
" PDF too slow "
…this topic will show up.
I hope you don’t mind.