I have an application that does a lot of basic CRUD functionality between various API’s, so at the minimum I have functions to connect to those services and then functions to wrangle the data depending on what the user wants to do.
I was wondering if there was any significant performance differences when Anvil jumps between functions that exist between two or more different server modules. Or if it exists then is there a point in which it would begin to be noticeable? Is there an argument for having one very long server file that contains everything I want in the back end?
From the Python point of view, executing a function that is imported from another module or defined on the same module makes no difference.
From the Anvil point of view, all the server modules are imported when an http endpoint is called, so there is no difference.
If you are using the “Keep server running” option (you definitely should), then the http endpoints will be faster because Anvil will only load the app after it has been modified (and once in a while after the server goes to sleep).
If you can’t use the “Keep server running” and you have some modules that are slow to load, then importing only on demand could help: @lazy_load_imports decorator (for slow loading imports)(I have not tried this). But this will improve performances only because it will skip some unused import. Executing imported functions has the same speed regardless of how they are imported.
Hey @vquach
I wrote it, and I don’t use it either, but it is pretty simple so I can’t think of a reason why it wouldn’t work. ( I just don’t have a use-case )
The best use case is if you are import - ing something that takes a noticeable amount of time to load, like tenserflow for example. Every time anvil spins up a new python environment for any server module function in the app even if it does not use tenserflow, the client will have to wait until the entire library loads on the server module.
This is just a quick fix for that particular purpose so you don’t have to edit a bunch of code you already wrote after you noticed a slowdown.