Python Forum

Full Version: Loop independent of excecution time of a script
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi

I have a script that needs to run every minute. I have used
time.sleep(60)
for this. Unfortunately the execution of the script takes different times. Once 5 seconds another time 10 seconds.

How can I program a loop without regarding the execution time? It should be executed every full minute.

Thanks in advance for your help!

Cheers
Forelli
see event timer example using sched here: https://docs.python.org/3.8/library/sched.html
Thanks for the link. Unfortunately I am not experienced with Python. Would it be possible to give me an example for a script that generates a "Hello World" every minute? 08:01:00pm, 08:02:00pm etc.
there are examples on that page.
Please try.
You can also post in the jobs section, please follow the rules for posting
Quote:Job offers require salary, wage, or sum amount within the post. Otherwise it will be deleted.
Ok. I don't think I'm in the right forum for beginners here. Can someone of the moderators please delete my account and this post too? This is missing in your backend. Thanks.
the sched module you can schedule an execution.

(Feb-02-2020, 01:59 PM)Forelli Wrote: [ -> ]Unfortunately the execution of the script takes different times. Once 5 seconds another time 10 seconds.
Are you saying that time.sleep(60) takes 5 seconds?
schedule work fine for this.
import time, datetime
import subprocess
import schedule

def job():
    print('Hello world')
    print(datetime.datetime.utcnow())
    # External call example
    #subprocess.run(['python', 'run_me.py'])

schedule.every(1).minutes.do(job)
while True:
    schedule.run_pending()
    time.sleep(1)
C:\code\home
λ python 1_min.py
Hello world
2020-02-02 22:09:45.740712
Hello world
2020-02-02 22:10:45.918306
Hello world
2020-02-02 22:11:46.057325
Hello world
2020-02-02 22:12:46.202572
Forelli Wrote:Can someone of the moderators please delete my account and this post too?
We do not delete posts or account.
Just leave and this Thread will go backward and disappear in the crowd Wink
Snippsat, can you enlighten me about the use of the lamda character? What's it's purpose?

C:\code\home
λ python 1_min.py
Hello world
2020-02-02 22:09:45.740712
Hello world
2020-02-02 22:10:45.918306
Hello world
2020-02-02 22:11:46.057325
Hello world
2020-02-02 22:12:46.202572
(Feb-02-2020, 10:44 PM)Dixon Wrote: [ -> ]Snippsat, can you enlighten me about the use of the lamda character? What's it's purpose?
It's just that i use cmder a better way for command line usage in Windows than using cmd/powershell.