I am having issues with loading speed on a particular app.
I have hard coded a dict into the server module. Then the form calls the server call on load (init) to test this and confirm the speed issues logging the time difference with datetime.datetime.now().
Code and blank material design form is identical in both apps as follows:
Client form, init:
s = datetime.datetime.now()
# Any code you write here will run when the form opens.
r = anvil.server.call('sc')
print('dict')
print(r)
print('time')
print(datetime.datetime.now()-s)
s = datetime.datetime.now()
# Any code you write here will run when the form opens.
r = anvil.server.call('sc')
print('time')
print(datetime.datetime.now()-s)
print('dict')
print(r)
My guess is that there are some slow imports in the Server Modules of your existing app. These have to be imported every time you do anvil.server.call, hence the difference between the apps. On the Business Plan and above, you can avoid this problem by enabling āKeep the Server Runningā.
Itās (b) - until Anvil has loaded all your Server Modules, it doesnāt know where the function youāre trying to call is defined! Thatās why persistent server modules can make such a big difference.
This looks like an opportunity for optimization. If the app hasnāt changed since the last time a @anvil.server.callable function was run, then the appās set of @anvil.server.callable points has not changed, either. If this set is saved, for Anvil to consult on the next call, perhaps it could avoid loading the uninvolved modules?
Thanks @daviesian, I understand where you are coming from and I will now look at the entire imports of my project to optimise/remove unused imports.
Interesting point @p.colbert, is this something that would have to be done by the Anvil Team or is this achievable within a project? If so, can you point me in the direction as to how to save the @anvil.server.callable points?
Now that you ask, @stu, I can roughly imagine a way to do it ourselves, but it greatly distorts the way we would have to edit and save (and debug?) our server-side code ⦠Iām not sure it would work, and Iām not sure the overhead would net any savings.
So as things stand, the Anvil Team would have to do it.
In fact, I was getting ready to post it in Feature Requests.
Note: the Anvil Team may have already tried it, and run into one āgotchaā or another. Or, perhaps, the overhead outweighed the savings, on average. At this writing, thereās no way to know.
In terms of finding the slow imports, am I correct in saying that multiple server modules importing the same set of imports (i.e. the standard anvil imports) only import that once, regardless of number server modules that import them?
i.e. in my optimisation here, can I safely ignore the following in all of my modules or do the multiple imports cause loading issues? :
import anvil.users
import anvil.email
import anvil.tables as tables
import anvil.tables.query as q
from anvil.tables import app_tables
import anvil.google.auth, anvil.google.drive, anvil.google.mail
from anvil.google.drive import app_files
import anvil.server`
As you suggested, I created a list of all imports, removed duplicates and added them to the speed test app I had made to understand this issue.
Main culprit immediately found: from plotly import figure_factory as ff
plotly figure_factory causes a minimum 1.5 second slowdown on every server call.
If it helps anyone, hereās a print out of my imports (as logged in Python 3 server code after import of anvil.server and datetime. Time starts before the first import and they are accumulative timings)