\$\begingroup\$
\$\endgroup\$
6
I want to execute a python script daily and thereby want to schedules task as follows.
The idea is that the script will run once daily, and the script will then decide what tasks to be done today.
The reason is that I can't use cron or windows scheduled task on my environment.
The question is that, is there any better idea?
from datetime import datetime
import os
from sheduled_tasks import daily_task1, daily_task2, weekly_task, monthly_tasks
tasks = {} # or open from saved pickel file
schedule = {
1: [daily_task1, daily_task2],
7: [weekly_task],
30: [monthly_tasks],
}
for delta, stasks in schedule.items():
for task in stasks:
name = task.__name__
if ((task not in tasks) or
(datetime.now() - tasks[name]).days >= delta):
task()
tasks[task.name] = datetime.now().replace(hour=6)
# pickle tasks
Rahul Patel
asked Jul 6, 2019 at 8:18
-
2\$\begingroup\$ does your script get executed once daily, or does it just sit on your OS as a process 24/7? \$\endgroup\$FirefoxMetzger– FirefoxMetzger2019年07月06日 12:44:34 +00:00Commented Jul 6, 2019 at 12:44
-
1\$\begingroup\$ What's your environment? Surely there's a better solution. \$\endgroup\$Reinderien– Reinderien2019年07月06日 13:37:10 +00:00Commented Jul 6, 2019 at 13:37
-
\$\begingroup\$ @FirefoxMetzger: Executed once daily. It doesn't sit on your OS as a process 24/7. \$\endgroup\$Rahul Patel– Rahul Patel2019年07月08日 04:20:30 +00:00Commented Jul 8, 2019 at 4:20
-
\$\begingroup\$ @Reinderien: Custom python (Linux based) environment without bash or anything. Just once daily python only task. I can request any python module from pypi. \$\endgroup\$Rahul Patel– Rahul Patel2019年07月08日 04:26:51 +00:00Commented Jul 8, 2019 at 4:26
-
\$\begingroup\$ I see. Should it be 'every week/month/year starting from the first invocation' or 'regularly in a weekly/monthly/yearly interval'? \$\endgroup\$FirefoxMetzger– FirefoxMetzger2019年07月08日 09:40:22 +00:00Commented Jul 8, 2019 at 9:40
1 Answer 1
\$\begingroup\$
\$\endgroup\$
3
You can get rid of doing any I/O and make your code cleaner in the process.
from datetime import datetime
from sheduled_tasks import daily_task1, daily_task2, weekly_task, monthly_tasks
schedule = {
1: [daily_task1, daily_task2],
7: [weekly_task],
30: [monthly_tasks],
}
# this could also be the date of first invocation, just change the timestamp
arbitrary_date = datetime.fromtimestamp(1234567890)
days_past = (datetime.now() - arbitrary_date).days
for delay, tasks in schedule.items():
if days_past % delay == 0:
[task() for task in tasks]
answered Jul 8, 2019 at 10:10
-
\$\begingroup\$ It took a while for me to understand. This is perfect for my continuously running server with power backup. I think for non-power backup server, it may fail. Thanks for simplification. \$\endgroup\$Rahul Patel– Rahul Patel2019年07月08日 11:42:41 +00:00Commented Jul 8, 2019 at 11:42
-
\$\begingroup\$ @RahulPatel That depends if you expect your server to fail for more than 24h. To comment on that, however, we would need more information about the actual environment than just the snippet and 'it will be executed once a day', because the latter is not the case if you have more than 24h downtime. \$\endgroup\$FirefoxMetzger– FirefoxMetzger2019年07月08日 12:47:06 +00:00Commented Jul 8, 2019 at 12:47
-
\$\begingroup\$ Agreed. Most of the time, the server is not dead for 24 hour. \$\endgroup\$Rahul Patel– Rahul Patel2019年07月09日 05:00:17 +00:00Commented Jul 9, 2019 at 5:00
lang-py