I am learning Python and was tinkering with Advanced scheduler. I am not able to gt it working though.
import time
from datetime import datetime
from apscheduler.scheduler import Scheduler
sched = Scheduler(standalone=True)
sched.start()
##sched.cron_schedule(second=5)
def s():
print "hi"
sched.add_interval_job(s, seconds=10)
i=0
while True:
print i
i=i+1
time.sleep(3)
sched.shutdown()
I am sure I am missing something basic. Could someone please point it out?
Also would you recommend a crontab to the advanced scheduler? I want my script to run every 24 hours.
Thanks
Standalone mode means that sched.start() will block, so the code below it will NOT be executed. So first create the scheduler, then add the interval job and finally start the scheduler.
As for the crontab, how about just sched.add_cron_job(s, hour=0)? That will execute it at midnight every day.
Related
I'm looking for some help with task scheduling in Python. I'm using schedule to run a script ("NQV5_0") at the same time each day (0930).
The below code works perfectly for what I need, except when it doesn't. It will work for 1-2 days in a row, but on the third day at 0930 nothing happens and I have to manually run NQV5_0. There is no error message, and the print statement keeps functioning.
Any suggestions as to what might be causing it not to start on the third day?
import schedule
import time
import importlib
launch_time = "09:30" # time to launch script
def job(): # imports script
importlib.import_module('NQV5_0')
schedule.every().day.at(launch_time).do(job)
while True: # below is what happens before launch time while waiting for scheduled job to run
schedule.run_pending()
print("\r Waiting to launch at", launch_time, "Time now:", time.ctime(), end="", flush=True)
time.sleep(1)
I have a scheduler_project.py script file.
code:
from apscheduler.schedulers.background import BackgroundScheduler
scheduler = BackgroundScheduler()
def func_1():
# updating file-1
def func_2():
# updating file-2
scheduler.add_job(func_1, 'cron', hour='10-23' , minute='0-58', second=20)
scheduler.add_job(func_2, 'cron', hour='1')
scheduler.start()
When I run, (on Windows machine)
E:\> python scheduler_project.py
E:\> # there is no error
In Log: (I have added logging in above code at debug level)
It says, job is added and starts in (some x seconds), but it is not
starting.
In task manager, the command prompt process display for a second and disappear.
And my files are also not getting updated, which is obvious.
What's happening? What is the right way to execute this scheduler script?
Scheduler was created to run with other code - so after starting scheduler you can run other code.
If you don't have any other job then you have to use some loop to run it all time.
In documentation you can see link to examples on GitHub and one of example uses
while True:
time.sleep(2)
I have a function, which is executed for every hour using threading module. But I have encountered problems with this,
i.e., the function is not running after couple of hours, like after 10 - 12 hours (This time is varying)
def update_df():
df = pd.read_sql(sql_query, connections['DB2'])
threading.Timer(60*60*1, update_df).start()
update_df()
Questions:
What is best practice, to implement this function such that it
should run for every IST hour(not on system time)?
Why threading module haven't worked properly (Is there any built-in module to do same job)?
Edit-1:
Question-1 is sovled
Question-2 need more visibility
Extending the answer of #Harley,
You need to use trigger='cron' instead of 'interval' to execute every hour
Document
interval : This should be used when you want to run a function after fixed number of hours/minutes/seconds irrespective of
day/month/year.
cron : This should be used when you want to run a function at a particular day/month/year at a particular hour/minute/second.
from apscheduler.schedulers.background import BackgroundScheduler
scheduler = BackgroundScheduler()
scheduler.configure(timezone='Asia/Kolkata')
scheduler.add_job(method, trigger='cron', hour='*') # Execute every hour independent of execution time(i.e., 1:00:00, 2:00:00, so on..)
scheduler.add_job(method_1, 'interval', minutes=30) # Execute on every 30 minutes depends program execution time
scheduler.start()
To stop scheduler, use .remove_all_jobs()
Ex: scheduler.remove_all_jobs()
you can use python apscheduler.
from apscheduler.schedulers.background import BackgroundScheduler
scheduler = BackgroundScheduler()
scheduler.configure(timezone='subcontinent/city')
scheduler.add_job(method-1, 'interval', seconds=10)
scheduler.add_job(method-2, 'interval', minutes=30)
scheduler.add_job(method-3, 'interval', hours=1)
//the below shceduler run mon-thu and sun on every o,15,30 and 45 minutes of each hour
scheduler.add_job(method, 'cron', minute='0,15,30,45', day_of_week='mon-thu,sun')
scheduler.start()
So this is something I have been wondering for a while, and while I don't know if there is a correct answer there is probably a better option.
So which of the options below is best to schedule a python script to run at a specific time? Let me know what you prefer, or if you have an alternative.
1) Take a python file script.py, write a ".bat" file to run the code in command prompt, and then use windows native task scheduler to launch the file at a specific time each day.
BAT Example:
cd C:\Users\Administrator\Documents\Scripts
python script.py
This is some code for a BAT file that will run your python script.
2) Use python to create a timed task within your file like some of the examples below:
import schedule
import time
def job():
print("I'm working...")
schedule.every(10).minutes.do(job)
schedule.every().hour.do(job)
schedule.every().day.at("10:30").do(job)
while 1:
schedule.run_pending()
time.sleep(1)
or
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()
or
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'])
When it comes to option 2 there are plenty of examples I could include but I just want to know whats better in your opinion.
Does one of the options use more processing power? Is one of the options more reliable? etc.
I will choose option 1. If you choose option 2, your code will not run in several circumstances, such as your machine restart or your python IDE crashes.
Option 1 will run your code as long as your machine is running.
I have never used Option 2 personally.
In general, if you just have one or two servers. Task Scheduler (Windows) or cron (Linux) would be the way to go.
There are also tools like AutoSys that are built for scheduling batch jobs.
Hello I am very new to programming and i'm trying to make an auto-posting bot for my subreddit.I'm using praw and I need to run this script at certain times and have it input and work
import praw
r = praw.Reddit(user_agent="UA")
r.login("username", "password")
sub = r.get_subreddit("Sub")
sub.submit("Title", text="Post text")
I'm running windows and someone said to use the task scheduler but I haven't been able to figure it out. Any help would be great. Thank You.
I would suggest to look into sched, a general purpose event scheduler. It is described, with appropriate examples to start you, in the Python's documentation.
Sample:
import time
import sched
scheduler = sched.scheduler(time.time, time.sleep)
def reddit():
<your code>
def scheduler_reddit():
scheduler.enter(0, 1, reddit, ())
scheduler.run()
time.sleep(3600)
for i in range(100):
scheduler_reddit()
Change 3600 with the desired time in seconds.