Python Forum
Loop independent of excecution time of a script - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Loop independent of excecution time of a script (/thread-24162.html)



Loop independent of excecution time of a script - Forelli - Feb-02-2020

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


RE: Loop independent of excecution time of a script - Larz60+ - Feb-02-2020

see event timer example using sched here: https://docs.python.org/3.8/library/sched.html


RE: Loop independent of excecution time of a script - Forelli - Feb-02-2020

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.


RE: Loop independent of excecution time of a script - Larz60+ - Feb-02-2020

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.



RE: Loop independent of excecution time of a script - Forelli - Feb-02-2020

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.


RE: Loop independent of excecution time of a script - metulburr - Feb-02-2020

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?


RE: Loop independent of excecution time of a script - snippsat - Feb-02-2020

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


RE: Loop independent of excecution time of a script - Dixon - Feb-02-2020

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



RE: Loop independent of excecution time of a script - snippsat - Feb-02-2020

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