Schedule Job with APScheduler Version 3 - python

I am trying to schedule a job in python using the APScheduler package. This answer looked great, but it's snytax is out of date. I went to user guide for the current version 3, but I cannot find a basic example where I can pass a datetime object to the scheduler like in the linked answer.
from datetime import date
from apscheduler.schedulers.background import BackgroundScheduler as Scheduler
from datetime import datetime
# Start the scheduler
sched = Scheduler()
sched.start()
# Define the function that is to be executed
def my_job(text):
print text
#Schedule job
job = sched.add_job(my_job, next_run_time = datetime(2015, 5, 11, 1, 05, 5), args = ['Hello World!'])
This yields the error: No handlers could be found for logger "apscheduler.executors.default".

Related

APScheduler to do jop every day withon 11:15am to 2:15 pm on 15 min delay

I need to do a job with 15 mins interval within 11:15 am to 2:15 pm IST every day,
I tried using the below code but still i cannot able to get it.
Any answers would be really appreciated.
from apscheduler.schedulers.background import BackgroundScheduler,BlockingScheduler
from datetime import datetime
def my_job():
print(f"custom job called at {datetime.now()}")
scheduler = BlockingScheduler()
scheduler.add_job(my_job, 'cron', minute='1', hour='12-14')
scheduler.start()
The easiest way I see to achieve those times would be to create multiple triggers with the OrTrigger. I also used different cron operators, to make it run every 15 minutes.
from apscheduler.triggers.combining import OrTrigger
from apscheduler.triggers.cron import CronTrigger
trigger = OrTrigger([CronTrigger(hour='11', minute='15-59/15'),
CronTrigger(hour='12-13', minute='0-59/15'),
CronTrigger(hour='14', minute='0-15/15')])
scheduler.add_job(my_job, trigger)
from apscheduler.triggers.combining import OrTrigger
from apscheduler.triggers.cron import CronTrigger
trigger = OrTrigger([CronTrigger(hour='11', minute='15-59/15'),
CronTrigger(hour='12-13', minute='0-59/15'),
CronTrigger(hour='14', minute='0-15/15')])
scheduler.add_job(my_job, trigger,timezone='Asia/Kolkata')
scheduler.start()

python apscheduler does nothing

I made the following ,but it doesn't print the time.
from apscheduler.schedulers.background import BackgroundScheduler
from datetime import datetime
def tick():
print('Tick! The time is: %s' % datetime.now())
scheduler = BackgroundScheduler()
scheduler.add_job(tick,'interval',seconds=3)
print('starting')
scheduler.start()
print('stopped')
This is because your program is exiting before the interval has elapsed and needs to be kept alive at least until the first interval, consider using the following example:
while True:
#Thread activity here (time.sleep(2) for example)
or using other forms of activity to keep your main thread alive. Or just print out the time without this scheduling if that's what you really need.

How to import a python file to be executed on schedule time. using schedule

I am trying to use schedule to schedule jobs every work day at a certain time and execute a file when its time. The problem is that it runs the validate.py on schedule and off schedule. I want it to run only when the time is 9:09 .
from dateutil import rrule
from datetime import datetime, timedelta
import time
import schedule
import requests
import validate.py
def job():
execfile("validate.py")
start_hour = datetime.strptime("09:09:26","%H:%M:%S")
end_hour = start_hour + timedelta(hours=7)
for t in rrule.rrule(rrule.HOURLY, dtstart=start_hour, until=end_hour):
schedule.every().monday.at(t.strftime("%H:%M")).do(job)
schedule.every().tuesday.at(t.strftime("%H:%M")).do(job)
schedule.every().wednesday.at(t.strftime("%H:%M")).do(job)
schedule.every().thursday.at(t.strftime("%H:%M")).do(job)
schedule.every().friday.at(t.strftime("%H:%M")).do(job)
while True:
schedule.run_pending()
time.sleep(1)
import pytz
from apscheduler.schedulers.blocking import BlockingScheduler
def job():
execfile("validate.py")
if __name__ == '__main__':
timez = pytz.timezone(YOUR_TIMEZONE)
scheduler = BlockingScheduler(timezone=timez)
scheduler.add_job(job, "cron", day_of_week="1-5", hour=9, minute=9)
scheduler.start()
BlockingScheduler might be useful on your case. the parameter: YOUR_TIMEZONE just like'Asia/Shanghai'

how to start a task at a specific time with django & celery

I'm using Celery, and it works for async, but I need to set up an operation to a specific datetime.
For example "on the 30th of August, 2019, at 11:35, do this."
My celery.py is very simple now, but it works:
import time
from datetime import datetime, timedelta
from datetime import date
from celery import shared_task,current_task, task
from celery import Celery
app = Celery()
#app.task
def test():
print ('1')
todaynow = datetime.now()
print todaynow
I call it from view and run print on rabbit
Any idea for how to program a task?
ty
EDIT:
I try in view to call "test"
test.apply_async(eta=datetime(2019, 7, 31, 6, 28))
in flower it receive the task but not execute it, why?
To run a task at a specific time you can pass the eta parameter to apply_async
test.apply_async(eta=datetime.datetime(2019, 8, 30, 11, 35))
Celery component responsible for scheduling tasks to run at specific time, or repeatedly after some time is called the Celery Beat (scheduler). Celery documentation has a complete section describing it, with details how to run it, and how to configure it. If you are familiar with crontab you will easily create your own scheduled task-runs.
You can create a single executed periodic scheduler for "the 30 of august 2019 at 11 and 35 min do this" by using celery such as:
import time
from datetime import datetime, timedelta
from datetime import date
from celery import Celery, shared_task,current_task, task
from celery.schedules import crontab
app = Celery()
#app.on_after_configure.connect
def setup_periodic_tasks(sender, **kwargs):
sender.add_periodic_task(
crontab(hour=11, minute=35, day_of_month=30, month_of_year=8),
test.s(),
expires=1
)
#app.task
def test():
print ('1')
todaynow = datetime.now()
print todaynow
To schedule task you need to use celery beat .
from celery.task import periodic_task
#periodic_task(run_every=crontab(minute="*")) # It will run your task every minute
def schedule_task():
print('1')
todaynow = datetime.now()
print(todaynow)
You can schedule your task at any specific time using periodic task .
To know more use this link
https://docs.celeryproject.org/en/latest/userguide/periodic-tasks.html .
Don't forget to restart your celery beat after creation of task .

Python script to do something at the same time every day [duplicate]

This question already has answers here:
How do I get a Cron like scheduler in Python?
(9 answers)
Closed 6 years ago.
I have a long running python script that I want to do someting at 01:00 every morning.
I have been looking at the sched module and at the Timer object but I can't see how to use these to achieve this.
I spent quite a bit of time also looking to launch a simple Python program at 01:00. For some reason, I couldn't get cron to launch it and APScheduler seemed rather complex for something that should be simple. Schedule (https://pypi.python.org/pypi/schedule) seemed about right.
You will have to install their Python library:
pip install schedule
This is modified from their sample program:
import schedule
import time
def job(t):
print "I'm working...", t
return
schedule.every().day.at("01:00").do(job,'It is 01:00')
while True:
schedule.run_pending()
time.sleep(60) # wait one minute
You will need to put your own function in place of job and run it with nohup, e.g.:
nohup python2.7 MyScheduledProgram.py &
Don't forget to start it again if you reboot.
You can do that like this:
from datetime import datetime
from threading import Timer
x=datetime.today()
y=x.replace(day=x.day+1, hour=1, minute=0, second=0, microsecond=0)
delta_t=y-x
secs=delta_t.seconds+1
def hello_world():
print "hello world"
#...
t = Timer(secs, hello_world)
t.start()
This will execute a function (eg. hello_world) in the next day at 1a.m.
EDIT:
As suggested by #PaulMag, more generally, in order to detect if the day of the month must be reset due to the reaching of the end of the month, the definition of y in this context shall be the following:
y = x.replace(day=x.day, hour=1, minute=0, second=0, microsecond=0) + timedelta(days=1)
With this fix, it is also needed to add timedelta to the imports. The other code lines maintain the same. The full solution, using also the total_seconds() function, is therefore:
from datetime import datetime, timedelta
from threading import Timer
x=datetime.today()
y = x.replace(day=x.day, hour=1, minute=0, second=0, microsecond=0) + timedelta(days=1)
delta_t=y-x
secs=delta_t.total_seconds()
def hello_world():
print "hello world"
#...
t = Timer(secs, hello_world)
t.start()
APScheduler might be what you are after.
from datetime import date
from apscheduler.scheduler import Scheduler
# Start the scheduler
sched = Scheduler()
sched.start()
# Define the function that is to be executed
def my_job(text):
print text
# The job will be executed on November 6th, 2009
exec_date = date(2009, 11, 6)
# Store the job in a variable in case we want to cancel it
job = sched.add_date_job(my_job, exec_date, ['text'])
# The job will be executed on November 6th, 2009 at 16:30:05
job = sched.add_date_job(my_job, datetime(2009, 11, 6, 16, 30, 5), ['text'])
https://apscheduler.readthedocs.io/en/latest/
You can just get it to schedule another run by building that into the function you are scheduling.
I needed something similar for a task. This is the code I wrote:
It calculates the next day and changes the time to whatever is required and finds seconds between currentTime and next scheduled time.
import datetime as dt
def my_job():
print "hello world"
nextDay = dt.datetime.now() + dt.timedelta(days=1)
dateString = nextDay.strftime('%d-%m-%Y') + " 01-00-00"
newDate = nextDay.strptime(dateString,'%d-%m-%Y %H-%M-%S')
delay = (newDate - dt.datetime.now()).total_seconds()
Timer(delay,my_job,()).start()

Categories

Resources