Scripts' Run-time Environments -- are my guesses accurate?

What I’m trying to do:
Fully understand Anvil Docs | Scripts. It seems to be a fairly recent addition.

Looks like a great way to to slim down the loaded set of Server Modules, by factoring out the occasional background task, or other code-heavy (and less-used) application features. I might try it out some time soon.

What I’ve read and what’s not clear:

Scripts execute in the trusted server environment

Well, yes, but which one? In many Apps I have Dev, Test, and Prod, plus the IDE-only Debug environments. Some apps may have customer-specific environments, as well.

A reasonable guess would be as follows:

  • If the script is invoked from the IDE, then it runs in the Debug Environment. Otherwise,
  • If the script is invoked from a Server Module, then it runs in that Server Module’s Environment. Otherwise,
  • If the script is invoked from a Client Module, then it runs in that Client’s Server Module Environment.

What difference does this make? Well, if you’re going to

return rich data including … database records.

then it may be very important to know which Table(s) (i.e., from which database) those database records are coming from!

Not listed, and I’m guessing here, too:

  • If the script is invoked as a Scheduled Task, then it runs in each of the Scheduled-Task-Enabled Environments, in turn.
  • If the script is invoked by another script, then it runs in the parent script’s Environment.

It’s not clear whether this second script gets its own five-minute run-time, or is running on its parent’s clock.

My assumptions above are based on the principle of least surprise. That is, the script will use the same environment as the code that invoked it, or if no code invoked it, the Debug environment.

2 Likes

I wonder if you may have just pre-empted @meredydd by a few hours of an announcement, haha

More likely he pre-empted himself a bit earlier, with this:

Both new features are valuable, but Faster Storage will have a much broader, more visible impact, and that’s in keeping with the level of excitement expressed here:

You were in the User Group meeting where I live-coded my way through this, so you know the answer, but for everyone else:

A Script executes as a Background Task in your app. It runs in a separate Python process in the same environment a Background Task would: If you run it from the Anvil Editor it’s in your debug environment; if you launch it (with anvil.server.run_script() or anvil.server.launch_background_task()) from some other environment, that’s where it will run!

2 Likes