I currently run a Twitter bot. I want it to publish an image every ten minutes. I'm currently accomplishing this by using time.sleep(600)
. This has led it to end up posting things at irregular intervals. However, I'm wanting it to publish an image every ten minutes like clockwork. At 1:00pm, at 1:10pm, at 1:20pm, at 1:30pm, etc. What is the best way to accomplish this?
Asked
Active
Viewed 1,217 times
0

Gwendolyn97
- 27
- 1
- 6
-
run a cron that executes the script every 10 minutes. – d_kennetz Sep 21 '20 at 17:24
-
create a [cron-job](https://crontab.guru/) – neilharia7 Sep 21 '20 at 17:25
-
What OS? On most you can create a recurring timer. For example on Windows use Windows Task Scheduler – 001 Sep 21 '20 at 17:25
-
Sleep for 1 sec and check if it is time. Do your thing and mark the last time you uploaded as done. – Tarik Sep 21 '20 at 17:25
-
Also [here](https://stackoverflow.com/questions/22715086/scheduling-python-script-to-run-every-hour-accurately) is another existing question about this. – Random Davis Sep 21 '20 at 17:31
1 Answers
3
You should use Cron to execute your script.
*/10 * * * * python your_twitter_script.py
If you want to do it with Python only, sched or schedule are also a solution.
import schedule
import time
def job():
# twitter job
schedule.every(10).minutes.do(job)
while True:
schedule.run_pending()
time.sleep(1)

RobinFrcd
- 4,439
- 4
- 25
- 49