By default, in Anvil, the process that runs your server code is killed as soon as it has finished serving a request. This is for two reasons:
-
To prevent you accidentally “leaking” memory/threads/etc that get “left behind” but keep consuming resources
-
So we don’t have to keep your code running between requests, consuming CPU and memory. (Eg if you loaded a ton of data into RAM and we left your server code running, that’s RAM we can’t use for anyone else!)
However, if you are on a Business plan or above, you can change this. If you open the runtime options (at the top right of a server module), you will see a checkbox marked “Keep server running”:
This…does what it says on the tin! Your server process will keep running between server calls. This means that if you have costly setup operation (eg you need to load a machine-learning model from disk), you only pay that cost once, rather than on every call. This also means that background threads will continue running after your server function returns.
Each call runs on a separate thread, so your code will need to be thread-safe if you’re serving multiple clients concurrently!
Your server process will be killed and restarted when you update your app (after it’s finished any calls in progress) - so if you create a runaway thread by mistake, it will be terminated as soon as you fix your mistake.
Note: There can still be more than one process serving your app at any given time, for reasons of volume or redundancy. Do not rely on global variables being shared between all requests!