How to run the web2py scheduler on PythonAnywhere

The web2py scheduler is a way to process asynchronous jobs from web2py. The way to get it running on PythonAnywhere is to use a Scheduled Task, which runs once a day.

This is the usual long-running tasks hack on PythonAnywhere -- because we can't guarantee to keep a process running forever, the workaround is to have a scheduled task that runs periodically, and restarts the task if it's died, or just quits if it sees it's already running.

Here's some example code -- you'll need to adapt this to match your own web2py apps' names:

#/usr/bin/env python
import os
import socket
import subprocess
import sys
filename = os.path.abspath(__file__)  # we use this to generate a unique socket name

# we use a local socket as a lock.
# it can only be bound once, and will be released if the process dies
# we want to keep the socket open until the very end of
# our script so we use a global variable to avoid going
# out of scope and being garbage-collected
lock_socket = socket.socket(socket.AF_UNIX, socket.SOCK_DGRAM)

try:
    lock_socket.bind('\0' + filename)
except socket.error:
    print("Failed to acquire lock, task must already be running")
    sys.exit()

subprocess.call(["python", "web2py/web2py.py", "-K", "my_web2py_app_name"])

Save that code to a file in your home folder, it doesn't matter where, make it executable using a bash console like this:

chmod +x /path/to/your.file

...and then set it to run as a scheduled task -- once a day, or even once an hour if you have a paying account. For example, if you save it to /home/yourusername/run-web2py-scheduler-for-myapp.py, you can just enter that full path as the scheduled task.