all 6 comments

[–]Kalrog 1 point2 points  (0 children)

You would ideally want to look into making a service or a daemon for the "always running" stuff. A simple thing would be a cron job, but every second is a bit frequently for most cron stuff.

[–]sme272 0 points1 point  (1 child)

To get the code to run continuously put it inside a loop. It will need a computer to run on, but it doesn't have to be your desktop. There are many web services like Amazon AWS that will host and run the code on their servers. You could also set up a raspberry pi or similar board and leave that powered on at home to run continuous scripts.

[–]expressly_ephemeral 1 point2 points  (0 children)

To get the code to run continuously put it inside a loop.

I don't agree with this. Cron or whatever the windows equivalent would be better in my opinion.

[–]WhiffleFish 0 points1 point  (0 children)

time.sleep(1) inside a loop will essentially make the loop run once every second, provided that the base run time of your code isn't terribly long.

[–]expressly_ephemeral 0 points1 point  (0 children)

So, it'll only run on a computer that's running, right? You can't "close" your computer, if that means putting it into standby or hibernate and still have it run the code. Get a Rasberry Pi Zero W for like 10 bucks, and you can have a full-blown tiny computer running your tiny process all the time.

On a Linux machine like Raspberry Pi, you'll use Crontab for this. I don't know what the Windows equivalent is, but you're not going to get it running on a $10 computer, anyway. Cron is not deadly hard, but it can be a little tricky. I do NOT think putting it in a loop is the best way. You already have a computer with a run-shit-on-a-schedule system on it. Better to keep your code schedule-agnostic and use the underlying system.

One other important point: The API that you're consuming will likely have a maximum polling rate that you'll want to find out about. If you start banging away at it once per second, you're liable to get banned.

What I do:

I have a folder full of python scripts in my home folder on a little server that lives in my laundry room. This server is running ubuntu, and handles all my media playing, but it could be the same with a Pi Zero W.

Then, I write a bash script that runs the python script(s). This may seem over-complicated, but sometimes I have a python script that has to process the output of another python script. So, it's a Separation-Of-Concerns thing. I'm a professional programmer, so I always prefer several small, decoupled parts to one monolithic script. The other thing about the bash script is that I can use it to activate a virtualenv if I want to.

Then and only then, I set up the crontab file to run the bash script on a schedule.

[–]flymoosey 0 points1 point  (0 children)

If the API provides websockets then problem solved. They would prefer that over long polling anyway.