0

i need your help.

I need a non-bloking timer, that allows me, in the period it's still counting, doing other tasks. I need this function for my bot and obviously I don't want to block it all times I call this type of function which requires these timers.

So, in the past i used to use in Arduino (c++) the function millis() that in the same configuration seems not working well like

int t0 =0
int t1

void loop(){
t1= millis()
while (t1-t0 < 6000){
Serial.print(Timer!);
t0 = millis();}}

Do you have any advice for me? A code where I can start from? Thanks!

4
  • Have you looked into the time module? Commented Jul 19, 2021 at 16:48
  • Maybe Asyncio? stackoverflow.com/questions/45419723/… Commented Jul 19, 2021 at 16:48
  • Yeah i've been looking at this "time module" for a while but time.sleep is a blocking function Commented Jul 19, 2021 at 16:50
  • You can use time.time() or any of its variants for non-blocking, just like millis() in your example... Commented Jul 19, 2021 at 16:57

3 Answers 3

1

The accepted answer is fine for a single loop. The following will make a timer that can be used in a function call.

import time

class PassiveTimer:

    def start(self, duration):
        self.end_time = time.monotonic() + duration

    @property
    def end(self):
        return time.monotonic() > self.end_time

pt = PassiveTimer()

def func():
    if pt.end:
        print("timer")
        pt.start(5)

x = 0
pt.start(5)
while True:
    func()
    # do other things
    x += 1
    if x % 100_000 == 0:
        print('x', end='')
Sign up to request clarification or add additional context in comments.

Comments

0

The following will print "Timer" for 6 seconds:

import time

start_time = time.time() # returns number of seconds passed since epoch
current_time = time.time()

max_loop_time = 6 # 6 seconds

while (current_time - start_time) <= max_loop_time:
    # do stuff
    print("Timer")
    current_time = time.time()

1 Comment

Okay, it works but this is not what I managed to do. I needed a timer that does a task only once at expected timings. Just like. "5seconds. In 5 seconds i will print("Timer") then after other 5 seconds print(t"imer")
0

Okay, i found the solution by myself, trying to remember what i previously did on Arduino.

I based this answer from Adam Minas's one, but mine is quite different

So the expected behavior had to be: print(something) every 5 seconds so:

import time

start_time = time.time() # returns number of seconds passed since epoch
#current_time = time.time()
print(start_time)
max_loop_time = 20 # 6 seconds

while True:

    while (time.time() - start_time) > max_loop_time:
    
            print("Timer")
            start_time = time.time()

Then you can stop your while loop with break and other functions like

if == smth : 
    break

    

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.