-
Notifications
You must be signed in to change notification settings - Fork 972
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How can i run my task between some time? #231
Comments
+1 |
+2 |
I need to execute some job every day, every hour, start at '10:00' until '22:00'. The only way that I found do something like this is scheduling 2 task, one task that start every day at '10:00' that create another task executed by hour to 12 hours. import schedule
import time
import datetime
def job():
print("I'm working... " + datetime.datetime.now().strftime("%m/%d/%Y - %H:%M:%S"))
def createJob():
schedule.every(1).to(12).hours.do(job)
schedule.every().day.at("10:00").do(createJob)
while True:
schedule.run_pending()
time.sleep(1) The problem with this approach is the job are executed in random intervals. |
Hi @ciroanacleto
Nevertheless, take a look how you should initialize the CAPTURE flag |
Thanks by reply @BBarbosa import schedule
import time
import datetime
def job():
print("I'm working... " + datetime.datetime.now().strftime("%m/%d/%Y - %H:%M:%S"))
def createJob():
schedule.every(2).seconds.do(job).tag('second-tasks')
def cancelAllJobs():
schedule.clear('second-tasks')
schedule.every().minute.at(":05").do(createJob)
schedule.every().minute.at(":20").do(cancelAllJobs)
print(schedule.jobs)
while True:
schedule.run_pending()
time.sleep(1) The result was:
Note that the inner schedule job was executed on the interval between the two outer schedules at ':05' and ':20', that is, inside 15 seconds fits 7 executions in each 2 seconds. |
ok, I found some way to do this. import schedule
import time, datetime
def job():
print("I'm working %s"%(datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')))
def control_job():
tnow = datetime.datetime.now()
hm = tnow.strftime('%H:%M') #hour:minute like '10:00'
if hm in run_hour:
if run_hour[hm] == 0:
#run job and set the run flag to 1
job()
run_hour[hm]=1
#debug
print(run_hour)
#time < 10:00 and time> 22:00 reset all flag to 0
if ((hm <'10:00') or (hm >'22:01')):
for i in run_hour:
run_hour[i]=0
#global variable, set every hour as a run flag
run_hour = {'10:00':0, '11:00':0, '12:00':0, '13:00':0, '14:00':0, '15:00':0,
'16:00':0, '17:00':0, '18:00':0, '19:00':0, '20:00':0, '21:00':0, '22:00':0 }
schedule.every(59).seconds.do(control_job) #set less than 1 min. ensure compare the flag at least once per minute.
while True:
schedule.run_pending()
time.sleep(1) |
Thanks by reply @ChuanPoLee |
Hi, @ciroanacleto def job():
print("I'm working %s"%(datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')))
def control_job():
#run between Mon. to Fri. and 09 ~ 18 hour/day
tnow = datetime.datetime.now()
hh = tnow.strftime('%H') #hour like '09'
wk = tnow.isoweekday() #Mon =1
if (('08'<hh<'18') and (1 <=wk <=5)):
job()
schedule.every().hour.do(control_job) |
Nice ! |
My use case is slightly different but feels related. I want to run something once within a random time interval. To do so, I made this utility function to get a random time def randomTime(a='00:00', b='23:59'):
a_time = datetime.datetime.strptime(a, '%H:%M')
b_time = datetime.datetime.strptime(b, '%H:%M')
a_seconds = a_time.minute*60 + a_time.hour*60*60
b_seconds = b_time.minute*60 + b_time.hour*60*60
random_seconds = random.uniform(0, b_seconds - a_seconds)
random_time = (a_time + datetime.timedelta(seconds=random_seconds)).strftime("%H:%M")
return random_time Then, I can use
which needs to be scheduled daily itself (for example, every day at 1:00 am). |
It seems that the package has not function to execute a task from time to time.
Is there any pythonic method to achieve it?
Like..
The text was updated successfully, but these errors were encountered: