Uplink script issue

Hello, I am using anvil uplink but every time I run my app in anvil I have to run the uplink first in the system(IDE).So is there a way so that I do not have to run the uplink script every time or it runs as on I run the app.
As a solution I think about converting the script to .exe but the pyinstaller is also throwing an error.
Any idea?

If your uplink script is running on linux , i do this
“nohup python anvil-uplink-script.py &” , works like a charm.

If your running on windows , good luck …

It is showing error as no file or directory.

Why don’t you just have the script run all the time on the startup of your uplinked machine and use

anvil.server.wait_forever()

At the end of your script after you have declared all of your functions?

If you want the script to do something every time the app is started, put whatever that is in a function and make the function callable from the app using @anvil.server.callable and then call the function from the __init__ of the first form that opens in your app.

What I want is to publish the app to the client but without the script the app will not perform the function so,have to run the script in the IDE like jupyter notebook.
If there is something or some way so that the client just have to use the app.

On Windows, I’ve successfully used AlwaysUp. Their site has examples of how to run a Python script as a Windows Service, on whatever machine you choose to run the Uplink script. I think they’ve made it about as simple as it’s going to get.

As a service, the script starts running (on the Uplink machine) shortly after Windows starts running, whether you’re logged in or not. As a result, Windows can reboot your machine (as it often does during an Patch Tuesday update), and your script will be restarted automatically afterwards, whether you’re there or not.

Running any script as a Windows Service comes with some potential restrictions and caveats. Whatever window it runs in, is invisible. You can’t simply watch the window to see how it’s doing.

If you need to monitor the script to see when it crashes, etc., that’s included as part of AlwaysUp.

Like any tool, AlwaysUp comes with tradeoffs. It’s a commercial product. Incredibly inexpensive, for what it does. With a 1 month free trial.

As @ianbuywise says, anvil.server.wait_forever() will keep the script running.

However, if you want the script to restart after a crash/reboot/whatever then here’s one technique.

Have a cron job that runs every 2 minutes (or whatever period of time works for you) that starts the uplink script. Then protect your script from running multiple times by using a library like :

The docs explain how to use it, but here’s a snippet from my app :

from pid.decorator import pidfile
...
@pidfile()
def start_app():
  ...

if __name__ == "__main__": 
  try:
    start_app()
  except PidFileError:
    print("Already running!")

This is for Linux, but I imagine it would work on Windows (though you’d use the task scheduler rather that cron).

Yes, I use windows, the task scheduler is finnicky but once you get it to work once you don’t really have any problems.

Tips:

  1. If you need access to certain filesystem privileges you will need to create the task using an administrator account.
  2. You will need to check off “Run whether user is logged on or not” : image
    This unintuitively tells windows not to draw a new cmd window and just run it in the background instead.
  3. The action to do will be “start a program” Here is an example:
    image
  4. Then change all the “Where when why” stuff to your liking.
  • You put what python executable you want to run as the program
  • You put the name of your script as the ‘argument’ (if you have more arguments, they go after the script name)
  • You put where your script resides as ‘start in’ if you don’t want the current working directory to be your python path. (You could also put this any place you want your current working directory to be, that’s why its optional)
  • Be aware all of these are very weird with how windows handle quotation marks and paths

The last thing is once you get something working the way you want, you may just want to export the task to a file, and edit the XML directly because that is surprisingly slightly less M$FT kinds of insane. Then re-upload a new version for a different script / job / task etc.

You might want to save this task xml in your project file just for fun, if you want to load it on a different machine, you will have to change the USER in the xml before you import it, and windows will ask you to put in the password every time you make a change. It does not store this password in the XML. (for obvious reasons)

2 Likes

More windows tips to do the same thing @david.wylie was saying with a cron:

Here is an example task trigger for start every minute:

And here is an example ‘settings’ section, and how it protects it from starting your script 1400 times a day:
image

2 Likes

What about when you need some deliberate downtime, e.g., to update the Uplink program itself, or the things it depends on?

Something like this would make it stop from 5:03 AM to 7:03 AM for maintenance etc:


You could even create another task that runs a different backup/cleanup script after 5:03 AM, or use the same script with arguments passed into the action to make the script do something different, using sys.argv

To restart a script that I have made changes on I just kill the script process with windows task manager image
Because of the scheduled tasks it starts back up right away. If you don’t want it to start right away, right-click and ‘end all running tasks’ and then ‘disable this task’ it in the task scheduler first.

Right-click the columns bar in the Details tab of task manager and you can add “Command Line” so you can see which script python is actually running instead of just a bunch of python.exe's
image

2 Likes

@ianbuywise Thank you for the Windows Task Scheduler info.

It appears to be working perfectly.

Had some issues with running scripts that were located on a mapped network drive.

The solution was to use the UNC path to the script not the mapped drive letter.

Same with an ODBC driver the uplink was using. The path to the data file needed to be UNC as well.

I have the tasks set to repeat every minute and it just seems to work.

Also the editing of the XML export to set up similar tasks is a great help.

:+1: :beers:

:+1:
I also had trouble with logged in mapped network drives in windows before, since the user that was allowed to use the drive was not my administrator account (a Microsoft exchange server, the user isn’t even an account on my machine so…)
I was getting this error:
administrator cant share networked shares that are logged in with a non-elevated user

My workaround was this cli command run (as administrator) sometime after the machine is restarted but before the rest of the scripts run:

net use [drive letter]: \\[NETWORK ADDRESS]\[OPTIONAL EXTRA PATH] /persistent:yes

1 Like