import datetime
import schedule
import time
from datetime import datetime
def show_datafr1():
now = datetime.now()
current_time = now.strftime("%H:%M:%S")
print("Time =", current_time)
schedule.every(30).seconds.do(show_datafr1)
while True:
schedule.run_pending()
time.sleep(1)
The above code working fine. I am getting correct time every 30 seonds.
(Script run on google colab)
Problem is,
When I clicked stop and click run again. It actually run the script in two times.
like,
I clicked run script at 01second, (few seconds minutes later) I clicked stop.
If now I clicked run, said at 11 second, then it run as new schedule.
I am getting time at 01, 11, 31, 41 seconds in same script.
Can stop be really terminate the script? or any workaround?
or Can I resume it as resume the same script time??
I tried toolbar "runtime","restart runtime", every !pip need to reinstall again.
Thank you in advance.
Related
When writing the code, the following code will be activated on time, saying it is stopped until the hour.
However, to operate the code on time, it takes more than 59 minutes to run the code must run the code.
Is there a way to make it work at 12pm by adjusting the minutes and hours instead of seconds in that code?
enter code here`current_time = time.time()
time_to_sleep = (60 - (current_time % 60))
time.sleep(time_to_sleep)
Instead of sleep, you can pause until a defined time:
import pause
from datetime import datetime
pause.until(datetime(2022, 10, 25, 12))
or you can do it like this:
import datetime
import time
while datetime.datetime(2022,10,25,10,28,0) > datetime.datetime.now():
time.sleep(30)
pass
print("it's time to act")
but #Tanner version seems to be more preferable
I would like to start my code at the start of every hour I have tried using the minutes function of schedule however, the start time of the next process is dependent upon the end of the previous process and with the code I am trying to process the delay accumulates pretty quickly hence, I am trying this method instead. All help is appreciated. Thanks.
The code:
import schedule
import time
schedule.every().day.at("00:00").do(consolidated)
schedule.every().day.at("01:00").do(consolidated)
schedule.every().day.at("02:00").do(consolidated)
schedule.every().day.at("03:00").do(consolidated)
schedule.every().day.at("04:00").do(consolidated)
schedule.every().day.at("05:00").do(consolidated)
schedule.every().day.at("06:00").do(consolidated)
schedule.every().day.at("07:00").do(consolidated)
schedule.every().day.at("08:34").do(consolidated)
schedule.every().day.at("09:00").do(consolidated)
schedule.every().day.at("10:00").do(consolidated)
schedule.every().day.at("11:00").do(consolidated)
schedule.every().day.at("12:00").do(consolidated)
schedule.every().day.at("13:00").do(consolidated)
schedule.every().day.at("14:00").do(consolidated)
schedule.every().day.at("15:00").do(consolidated)
schedule.every().day.at("16:00").do(consolidated)
schedule.every().day.at("17:00").do(consolidated)
schedule.every().day.at("18:00").do(consolidated)
schedule.every().day.at("19:00").do(consolidated)
schedule.every().day.at("20:00").do(consolidated)
schedule.every().day.at("21:00").do(consolidated)
schedule.every().day.at("22:00").do(consolidated)
schedule.every().day.at("23:00").do(consolidated)
while True:
schedule.run_pending()
time.sleep(1)
The schedule package has an option for doing a task every hour:
import schedule
import time
schedule.every().hour.do(consolidated)
while True:
schedule.run_pending()
time.sleep(1)
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 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.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
What is the best way to repeatedly execute a function every x seconds in Python?
Hi so here is the code I have:
client = myclient(info1,info2)
sellor()
Contractor()
It works perfectly but what I would like to do is to make python launch that code every 60 seconds indefinitely...
I don't actually understand how I have to put the code together with the time loop
Any help is appreciated
Thank's
If the 60 seconds ignores the time it takes to execute your code):
from time import sleep
while True:
sleep(60)
# your code here
but if the 60 seconds takes into account the time it takes to execute your code:
from time import sleep
from os import fork
while True:
sleep(60)
fork() # create child process
# your code here
Use the sleep method. Just create a loop (while, for, whatever) and sleep for 60 secs every iteration.
import time
while True:
client = myclient(info1,info2)
sellor()
Contractor()
time.sleep(10)
hope it works,all the best mate
import time
repeat_time = 3.0
while True:
start_time = time.time()
# Your code goes here
time.sleep(max(repeat_time - (time.time() - start_time), 0.0))
And your code will be executed exactly every "repeat_time"
You could use sleep as already mentioned. But because there may be a variable amount of time needed for your own functions to run, this wouldn't necessarily mean your functions are run every 60 seconds.
If it was important that the period between each start of your functions is closer to 60 seconds, you could use time. I haven't tried this but something like
import time
while True:
# Get the current time
startTime = time.time()
# Your functions
client = myclient(info1,info2)
sellor()
Contractor()
delay = True
while delay:
if time.time() - startTime > 60:
delay = False # Break the delay
You might also think of just scheduling the task through windows scheduler. The benefit here would end the script once run and then execute the script again after scheduled interval. In the second approach it seems that the script instance process would continually run and only use the sleep function to do nothing for the specified time. I take it this way if the scripts fails at any instance you might have to keep a check to restart the script. While as a scheduled activity the script will be executed in any case at that specified intervals.
You might also not want the process thread to be kept running for the python script executed. I will research on this and you might get to hear form our other folks in the mean while.
Regards,
Harshal