Forums

Long running task tutorial?

Hi there,

I'm looking to setup a script to run for weeks, if not months. Right now, it tends to terminate unexpectedly and requires a manual restart.

https://help.pythonanywhere.com/pages/LongRunningTasks

I found this explanation solid, thanks very much - but had a few n00b questions.

Instead of importing my_module as described, I'm likely going to inject the socket code into my existing long running script.

  • How should I then setup the scheduled task to make sure that the script continues to run - i.e do nothing if socket is open, else run the script again?
  • I am not too familiar with the socket module, can I assume that if my long runningscript exits the socket will be closed/unbound automatically?

Thanks.

Hi radiusvector,

That is_lock_free() function is what checks to see if socket is available. and so you can run the script if it is, and do nothing if it is not.

You can assume that the socket will be closed automatically if your long running script dies.

Thanks conrad! Just so I fully understand, you're suggesting something like:

if (socket is active):
      do_nothing()
else: 
      long_running_script()

And putting that whole script on a scheduled task to run hourly?

Yup. That's the same as this line in the example:

if not is_lock_free():
    sys.exit()

which just ends if socket is already locked/bounded to (ie. your script is already running).