masbro
masbro

Reputation: 374

Running Python script continously 24/7 on the cloud

Is there any solution for my needs? Basically, I'm running the script that runs a certain function with APScheduler. So the script will run a function every 5 minutes.

PythonAnywhere can't do that because they don't support the APScheduler.. And it's very bad practice if we use an infinite loop on PythonAnywhere.

Upvotes: 4

Views: 6975

Answers (2)

J0ANMM
J0ANMM

Reputation: 8551

You can try Heroku and its Worker Dynos.

I found it much simple to set up than other services like AWS or Google Cloud Platform. You can have a basic account for free and play around.

They have as add-on Heroku Scheduler, which would allow you to avoid the infinite while.

Heroku Scheduler

Run scheduled tasks every 10 minutes, every hour, or every day. Starting at $0/mo.

Run Jobs with Ease

Scheduler is an add-on for running jobs on your app at scheduled time intervals, much like cron in a traditional server environment.

Upvotes: 3

zglin
zglin

Reputation: 2919

You can do it with either a cronjob or a helper script inside an infinite while loop.

In option 1, you would type in crontab -e and you would setup a cronjob. Once in the cron editor, you can set the frequency for various frequencies. For a 5 minute cronjob this code should work

*/5 * * * * /usr/bin/python /home/yourscript.py

For option 2, you would setup some variation of the command below as a python script

import time
......

while True:
# your function
   function()
   sleep(300)

The sleep command will pause execution for 5 minutes.

I would caution you that running an infinite while loop on PythonAnywhere will quickly burn through your CPU time allocation (depends on how efficient code you write).

Upvotes: 2

Related Questions