I've a main function with some code and i need run this every a predetermined time, but independent of the time that i configure it run every 2-3 minutes. I don't have an idea what's going. I'm show some example below.
import schedule
def main():
print('Some code here...')
schedule.run_pending()
# the function main should be run every 30min...?
schedule.every(30).minutes.do(main)
schedule.every().hour.do(main)
main()
For what i researched this code shoud run every 30 minutes, but it run every 2-3 minutes.
You should not call your scheduled function directly. In your desired scenario, the function should run every X minutes - Meaning that the script that is responsible for running it, should run all the time, deciding when to invoke the function. a while True should do.
import schedule
def main():
print('Some code here...')
# the function main should be run every 30min...?
schedule.every(30).minutes.do(main)
schedule.every().hour.do(main)
while True:
schedule.run_pending()
Related
I have a job to schedule every day at particular time (let's say 11 am). I am using this code to do that. But it is not executing the job at specified time. If I try to execute it after every 4 or 5 seconds, it works. Does anyone know why it is not working when I am specifying a certain time to execute it? Here is the code that I am using.
import schedule
import time
def show_it():
print('hello')
schedule.every().day.at("11:00").do(show_it)
while 1:
schedule.run_pending()
time.sleep(1)
I would like to use the schedule library in to order to run a filename.py every day at the same time (17:00). I would like to run this schedule in the background. The example given in the documentation with my own criteria is as follows:
import threading
import time
import schedule
def run_continuously(interval=1):
"""Continuously run, while executing pending jobs at each
elapsed time interval.
#return cease_continuous_run: threading. Event which can
be set to cease continuous run. Please note that it is
*intended behavior that run_continuously() does not run
missed jobs*. For example, if you've registered a job that
should run every minute and you set a continuous run
interval of one hour then your job won't be run 60 times
at each interval but only once.
"""
cease_continuous_run = threading.Event()
class ScheduleThread(threading.Thread):
#classmethod
def run(cls):
while not cease_continuous_run.is_set():
schedule.run_pending()
time.sleep(interval)
continuous_thread = ScheduleThread()
continuous_thread.start()
return cease_continuous_run
def background_job():
exec(open('/Users/filename.py').read())
schedule.every().day.at("17:00").do(background_job)
# Start the background thread
stop_run_continuously = run_continuously()
# Do some other things...
time.sleep(10)
# Stop the background thread
stop_run_continuously.set()
However, when I run this code nothing seems to happen for me. I'm unsure what values to put in the interval= and time.sleep() to suit my need. I would be very grateful if someone could clarify this for me.
I am using below code to excute a python script every 5 minutes but when it executes next time its not excecuting at excact time as before.
example if i am executing it at exact 9:00:00 AM, next time it executes at 9:05:25 AM and next time 9:10:45 AM. as i run the python script every 5 minutes for long time its not able to record at exact time.
import schedule
import time
from datetime import datetime
# Functions setup
def geeks():
print("Shaurya says Geeksforgeeks")
now = datetime.now()
current_time = now.strftime("%H:%M:%S")
print("Current Time =", current_time)
# Task scheduling
# After every 10mins geeks() is called.
schedule.every(2).minutes.do(geeks)
# Loop so that the scheduling task
# keeps on running all time.
while True:
# Checks whether a scheduled task
# is pending to run or not
schedule.run_pending()
time.sleep(1)
Is there any easy fix for this so that the script runs exactly at 5 minutes next time.
please don't suggest me to use crontab as I have tried crontabs ut not working for me.
I am using python script in different os
your geeks function will cost time to execute,and schedule job start calculate 5min after geeks done,that's why long time its not able to record at exact time.
if you want your function run at exact time,you can trying this:
# After every 10mins geeks() is called.
#schedule.every(2).minutes.do(geeks)
for _ in range(0,60,5):
schedule.every().hour.at(":"+str(_).zfill(2)).do(geeks)
# Loop so that the scheduling task
It's because schedule does not account for the time it takes for the job function to execute. Use ischedule instead. The following would work for your task.
import ischedule
ischedule.schedule(geeks, interval=2*60)
ischedule.run_loop()
I am trying to write a script that executes every hour
but, when I run it... it takes an hour to run the job for the first time and then, it starts running like every 5 seconds
I don't understand what am I doing wrong here
from apscheduler.schedulers.blocking import BlockingScheduler
def job():
print('excuting job')
scheduler = BlockingScheduler()
scheduler.add_job(job, 'interval', hours = 1)
scheduler.start()
this is another code that I have used, but it's the same result
schedule.every().hour.do(job)
while True:
schedule.run_pending()
time.sleep(20)
Don't do it in python.
Make a file called exehour.bat with this (assuming you're using Windows):
#echo off
cd DIRECTORY-OF-FILE
:loop
timeout 3600
FILE-NAME
goto loop
So this will execute a file every 1 hour when you run it
Or if you need it in python
import time
def runhour():
#YOUR PYTHON CODE GOES UNDERNEATH HERE
#
time.sleep(3600)
Might have something to do within your def, like a rogue brake or other bad while loop or sleep timer. Maybe the checking job timer not being short enough is affecting something as well?
import schedule # I'm not sure about the BlockingScheduler import but this one works for me
import time # Hate how we need an import for this and it's not built into python....
def job():
print('executing job')
# Run your job on first boot without the scheduler
job()
# Then setup the schedule to be used from now on
schedule.every().hour.do(job)
# Check every second if we can do our job yet or not
# Bonus heartbeat . so you know the while loop is running and not crashed.
while True:
schedule.run_pending()
print(".", end="", flush=True)
time.sleep(1) # seconds
If it runs our job() then gets stuck, boot loops or crashes then its in your job def.
if it runs fine the first time, sets up the schedule fine but then gets stuck with executing the schedule then I don't really have any ideas without seeing more of the job() def..
for shits and giggles you could pip uninstall and reinstall schedule again and see if that helps idk..
I have a while loop which executes two methods. I want the functionB() to execute only EVERY 2 seconds.
I know there are solutions which can use the thread to use a timer to execute it every two seconds, but that is something that I DO NOT want to use. I want both methods to run on the MAIN thread.
def functionA():
# Code goes here
def functionB():
# Code goes here
while True:
# Execute function A
functionA()
# Periodically execute functionB every 2 seconds
functionB()
I am not sure on how to calculate the difference between the last time it executed and the current time. I search online for a few examples but they seem to confuse me more.
Any help would be appreciated.
get the timestamp in seconds and check if 2 or more seconds have passed since the last execution.
import time
def functionA():
# Code goes here
def functionB():
# Code goes here
lastExec = 0
while True:
# Execute function A
functionA()
now = time.time()
if now - lastExec >= 2:
# Periodically execute functionB every 2 seconds
functionB()
lastExec = now