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)
Related
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.
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 trying to place a call 'on hold' in asterisk using a python AGI script, the function will check if the person is available, when he is asterisk will dial the person if he is not the script should wait 10 seconds before checking availabilty again and dial when the person is available. However I ran into a small problem, using the time.sleep(10) function hangs up the call when a person is not available, I expect this is because the thread the script runs on will sleep and asterisk thinks the script is done running and hangs up the call. Removing the time.sleep() gives me what I want without the time interval.
agi = AGI()
agi.verbose("python agi started")
place_call_on_hold("SIP/6001")
def place_call_on_hold(s):
agi.verbose("entered place_call_on_hold function")
while True:
status = agi.get_variable('DEVICE_STATE(%(redirect)s)'%{'redirect':s})
agi.verbose(status)
if status == 'NOT_INUSE':
agi.verbose("Info: place_call_on_hold: calling the number")
agi.appexec("Dial",s)
else:
agi.verbose("Info: place_call_on_hold: sleeping for 10 sec")
time.sleep(10)
Is there a way to wait 10 seconds without using the sleep() function or how can I make sure the call won't end before the time.sleep wakes back up?
why not try time difference before condition and after condition something like this
import time
start = time.time()
end = time.time()
while (end-start) < 10:
end = time.time()
print(start, end, end-start)
I fixed my problem by just calling the Asterisk Wait(10) function instead of time.sleep(10).
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.
import time
number = 1
while number > 0:
print "Testing"
time.sleep(5) #print every 5 seconds
That is just an example loop. I'm a semi-beginner and I'm not sure how to make a keypress(any key is fine) display how long the program has been running. This program will be running on Windows 7 and Linux.
Thank you very much.
Welcome to Stack Overflow and to Python! You'll like it here.
First, I'll show you how to print out the time your code has been running. The time module includes a time() function that gets the current time as a Unix timestamp (the number of seconds since January 1, 1970). If you assign it to a variable at the start of the function, you can simply call it each time through the loop and subtract it from the current time to get your runtime. With me so far?
(You can also remove your number variable and the number > 0 check and simply replace it with True.)
import time
start_time = time.time()
while True:
print "I've been running for %d seconds!" % (time.time() - start_time)
time.sleep(5) #print every 5 seconds
But you asked how to get it each time the user presses a key. If you just want 'enter', you can do:
import time
start_time = time.time()
while True:
print "I've been running for %d seconds!" % (time.time() - start_time)
raw_input("Press Enter...")
The raw_input() function will wait for the user to press Enter, then print out the runtime.
One problem at a time.
How do you find how long your program has been running at the point at which you want to calculate?
How do you detect a key-press?
How do you get the program to produce 1) when 2) happens?
Try each problem in turn, then ask if you need help.
There are a lot of complexities and approaches for a such a simple question.
If you are looking for up time of a currently running process, use the OS to query that process with the subprocess module to run a command line operation such as 'ps | grep "foo" '
Usually programs do only one thing at a time. For example, the code could either do work OR look for a keypress. If you need to run have to run two different sections of code concurrently, spawn (run) the code segments as separate threads . For your question you could spawn two threads (pieces of code), one to do work and one to query up time.
Use the threading module in python to wrap the worker function and create a query thread. However, when the worker thread is done, you will want the query thread to terminate also. One way to do this is to define it as a daemon thread. Daemon threads terminate when they are the only threads alive.
For example:
from time import sleep
import datetime
import threading
def do_someting():
MAX_RUN_TIME = 300 #Seconds
for i in xrange(MAX_RUN_TIME):
print i,
sleep (1)
class worker_thread(threading.Thread):
def run(self):
do_someting()
class keypress_daemon_thread(threading.Thread):
def __init__(self):
threading.Thread.__init__(self) # Initialize the parent class
self.daemon = True # Terminates if only daemon threads are left
def run(self):
startTime = datetime.datetime.now()
while True:
raw_input()
timeDelta = datetime.datetime.now() - startTime
print 'Up for', timeDelta
if __name__ == '__main__':
workerThread = worker_thread()
keyPressThread = keypress_daemon_thread()
workerThread.start()
keyPressThread.start()
workerThread.join()