Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Timing of a while loop
#1
I have a while loop that is happening every 30 seconds

def modelTimer():
    starttime = time.time()
    timeout = time.time() + 60*60*6.5
    while time.time() <= timeout:
        model(app)
        time.sleep(30 - ((time.time() - starttime) % 30.0))
Is there any way I can execute code on the beginning of the next while loop?

Lets say I am printing ("Hello") every 30 secs.

"Hello" 00:30
"Hello" 01:00
"Hello" 01:30

ect ect.

If at a certain time, I want to add a "Hello, Patrick". How would I specify that with out just using another time.sleep timer. How do I not make it time based, like how do I make it "next while loop" no matter what the time different is. Because lets say if the trigger for my "Hello Patrick" happens at 1:15, and I want "Hello Patrick" to appear at 1:30 (the beginning of the 30 sec Hello loop).. I understand I can just time.sleep(15) or whatever. How do I not become dependent on time.

Like "when true: next while loop "hello patrick". No matter how many seconds untill it comes.

I'm sorry that was the best way I could describe what I am trying to do. That was the best way I could articulate it. The trigger to print "Hello Patrick" can happen at anytime in the 30 secs.

Talk soon thank you! Sorry, I tried my best to describe.

Pray
Reply
#2
What are you trying to do? I think you are making a mistake asking how to implement a solution you came up with instead of asking for the best way to solve your problem. Sleep is not a very good way to schedule things.
buran likes this post
Reply
#3
I found your question very confusing. Is this the type of thing that you are looking for?

from time import sleep

def show_output (counter) :
	reference = ('00:30', '01:00', '01:30', '02:00', '02:30')
	if counter == 2 :
		print ('"Hello, Patrick"', reference [counter])
	else :
		print ('"Hello"', reference [counter])

def modelTimer () :
	counter = 0
	wait_time = 3 # or 30
	while counter < 5 :
		show_output (counter)
		counter += 1
		sleep (wait_time)

modelTimer ()
Reply
#4
I believe the OP would like a timer where you can insert an event to be scheduled before an event that is already in the timer. That isn't something you can do with sleep, mostly because you are busy sleeping.

There are many ways to do this, but the choice is decided by what you are scheduling. If you want to schedule an event in a GUI you use the tools built into your GUI toolkit. Outside a GUI I would look at using the event scheduling library (sched) or maybe a threading event.
buran likes this post
Reply
#5
Yeah sorry about the confusing question, I tried my best to articulate it.

I’ll go through the above code this afternoon.

I was thinking this might work for me as well:

function_one():
   if reset == true:
      time.sleep(30)
      a = b

   if a = b:
      Do_something
Function one is looping on a main timer every 30 sec, so the code is being run through on the 30 sec, every minute. If I make the flag called “reset”. No matter where we are in the 30 seconds, the function_one code will always wait for the start of the 30 sec, then we sleep for the flag, then a=b, then function_one runs again and a=b so do_something is executed.

I think that might serve my purpose. Sorry I’m super amateur at coding.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question Timing actions with Python dangermaus33 0 1,018 Apr-19-2022, 10:08 PM
Last Post: dangermaus33
  Inconsistent counting / timing with threading rantwhy 1 1,775 Nov-24-2021, 04:04 AM
Last Post: deanhystad
  Synchronization/Timing Problem quest 5 3,013 Mar-31-2021, 10:26 PM
Last Post: quest
  Assigning Data from one column to another with different associated timing interval alexafshari 1 1,965 Apr-30-2020, 03:59 PM
Last Post: pyzyx3qwerty
  Frequency and timing of psycopg2 commits acecase 0 2,151 Nov-01-2019, 05:50 PM
Last Post: acecase
  Perpetual timing Mark17 3 2,909 Oct-24-2019, 03:46 PM
Last Post: Gribouillis
  Timing input Mark17 2 2,308 Oct-23-2019, 08:25 PM
Last Post: Mark17
  Timing functions with multiprocessing mntfr 3 5,038 Nov-18-2018, 06:00 AM
Last Post: woooee
  GPIO output timing help needed skid 5 4,099 Jan-23-2018, 04:12 PM
Last Post: skid
  change timing on py script kwfreverie 2 3,145 Dec-16-2017, 07:35 PM
Last Post: kwfreverie

Forum Jump:

User Panel Messages

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