Python Schedule library for scheduling whole script - python

How can I schedule a whole script using schedule library in Python, not just a function.
Like if i am writing something like
import schedule
from datetime import time,timedelta,datetime
import time as tm
from time import sleep
def mis_mes():
print('Hello')
schedule.every().second.do(mis_mes)
while True:
schedule.run_pending()
tm.sleep(1)
Then it works very well, but only with a single function and, as I said, I want to schedule the whole script.

Related

Schedule - Run a job until a certain time Python

Hello and thank you in advance. I am trying to write simple code to run a function for a specified duration. I basically copied this verbatim from schedule module docs (just changed units) but for some reason cannot get it to work.
import schedule
from datetime import datetime, timedelta, time
def job():
print('Hello World')
schedule.every(5).seconds.until(timedelta(seconds=20)).do(job)
I am not getting any errors, but the console will not print 'Hello World'. It has to be something simple but I cannot figure it out as I am very new at this.
When using schedule, you will need a while loop to keep the program running. Modify your code like this will solve the issue.
import schedule
from datetime import datetime, timedelta, time
import time
def job():
print('Hello World')
schedule.every(5).seconds.until(timedelta(seconds=20)).do(job)
while True:
schedule.run_pending()
time.sleep(1)

in python, how to pause until certain time only without date in pause library

I have python script and I want it to run at 10AM. I tried using with pause and datetime library like the code below
import pause
import datetime
pause.until(datetime.datetime(2021,1,6,10,00))
that means the script will pause until 6 January 2021 10.00 AM. What I want is, how to pause only certain time only the hour and the minute and seconds without putting the date there? is it possible?
Check out this library. I think it will suit your needs: https://pypi.org/project/schedule/
You can code things like:
import schedule
import time
def job():
print("I'm working...")
schedule.every().day.at("10:30").do(job)
while True:
schedule.run_pending()
time.sleep(1)

Python schedule not running as scheduled

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()

Display running time while a method is running in python

I have a method that takes long time to run. It has no iterables to use any progress bars like tqdm. While running, I would like it to display time elapsed with some progress graphic (something similar to package installers in Unix/Linux systems do with rotating / or some graphic ...). In the following dummy code
from time import sleep
def longTimetakingmethod():
sleep(3600)
showgraphictime()
longTimetakingmethod()
The showgraphictime() method should cause display something like
Calculating.../ Time elapsed: 00 hrs:15 mins:05 s
Is there a simple way to implement showgraphictime() ?
Or does it have to be a wrapper like showrunningupdate(longTimetakingmethod())
from time import sleep
def longTimetakingmethod():
sleep(3600)
showrunningupdate(longTimetakingmethod())
This library could be useful for you: Beautiful terminal spinners in Python
Example:
from halo import Halo
from time import sleep
def rocket_launch():
spinner = Halo({'spinner': 'shark'})
spinner.start()
for c in range(10, 0, -1):
spinner.text = 'Launching in {} seconds'.format(c)
sleep(1)
spinner.succeed('Rocket launched')
rocket_launch()
You can use Timeit module to map time taken for that and one of the spinners as suggested by #pbacterio

python schedule recurring event given only time in am/pm format

I am trying to to set a recurring event (running a python script) at a set time (9am EST, US in this case). The only way I found to do this was to manually calculate the miliseconds for the first time and increment them by 24 hours to schedule the next day. Is there a better way?
#!/usr/bin/python
import sched
import time
time_in_ms=???
scheduler = sched.scheduler(time.time, time.sleep)
def my_event(name):
import room_light.py
print 'START:', time.time()
scheduler.enterabs(time_in_ms, 1, my_event, ('',))
scheduler.run()
You can just use cron jobs. This is the best way to schedule tasks.
Update:
it's an OS feature, not Python.

Categories