Python Forum

Full Version: Timing of a while loop
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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
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.
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 ()
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.
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.