Python Forum
Loop independent of excecution time of a script
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Loop independent of excecution time of a script
#1
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
Reply
#2
see event timer example using sched here: https://docs.python.org/3.8/library/sched.html
Reply
#3
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.
Reply
#4
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.
Reply
#5
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.
Reply
#6
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?
Recommended Tutorials:
Reply
#7
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
Reply
#8
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
Reply
#9
(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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Button to stop while loop from another script Absolutewind 5 882 Sep-25-2023, 11:20 PM
Last Post: deanhystad
  Understanding venv; How do I ensure my python script uses the environment every time? Calab 1 2,258 May-10-2023, 02:13 PM
Last Post: Calab
  [SOLVED] [Linux] Script in cron stops after first run in loop Winfried 2 924 Nov-16-2022, 07:58 PM
Last Post: Winfried
  Clock\time calculation script Drone4four 3 1,462 Jan-21-2022, 03:44 PM
Last Post: ibreeden
  Real-Time output of server script on a client script. throwaway34 2 2,047 Oct-03-2021, 09:37 AM
Last Post: ibreeden
  how to loop a script? ZYSIA 1 1,998 Jul-22-2021, 06:46 AM
Last Post: Gribouillis
  How to measure execution time of a multithread loop spacedog 2 2,864 Apr-24-2021, 07:52 AM
Last Post: spacedog
  PyCharm Script Execution Time? muzikman 3 8,437 Dec-14-2020, 11:22 PM
Last Post: muzikman
  Updating a matrix in a time interval inside a for loop vp1989 4 2,888 May-17-2020, 07:15 PM
Last Post: vp1989
  for loop script over telnet in Python 3.5 is not working abhijithd123 1 2,895 May-10-2020, 03:22 AM
Last Post: bowlofred

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020