0

I need a way to print a timer every second and execute an action every 10 seconds. The output of the program should be like below.

Timer is 1
Timer is 2
Timer is 3
Timer is 4
Timer is 5
Timer is 6
Timer is 7
Timer is 8
Timer is 9
Timer is 10
Action is executed
Timer is 1
Timer is 2
Timer is 3
Timer is 4
Timer is 5
Timer is 6
Timer is 7
Timer is 8
Timer is 9
Timer is 10
Action is executed
Timer is 1
Timer is 2
Timer is 3
. . .

The program should use threading. It should not be an infinite while loop.

I could have done it with below code but it uses a global variable. How can I do it without using a global variable and with a small amount of code like below.

import threading
import time

global MytTimer
MytTimer=0
def foo():
    global MytTimer
    MytTimer=MytTimer+1
    print("Timer is " + str(MytTimer))
    threading.Timer(1, foo).start()
    if MytTimer >= 10:
        MytTimer=0
        print("Action is executed")        

foo()
2
  • Create a class with foo as a method and MytTimer as a member variable. Commented Jan 24, 2023 at 17:00
  • please show me how to do it. Commented Jan 24, 2023 at 17:06

2 Answers 2

1

I did it by creating a class.

import threading
import time

class count():
    def __init__(self, MytTimer):
        self.MytTimer = MytTimer
        self._run()
    def _run(self):
        threading.Timer(1, self._run).start() 
        self.MytTimer += 1
        print("Timer is " + str(self.MytTimer))
        if self.MytTimer >= 10:
            self.MytTimer=0
            print("Action is executed") 
        
a=count(MytTimer = 0) 
Sign up to request clarification or add additional context in comments.

Comments

0

You could create a ticker thread that delivers values 1-10 to a queue, and then a consumer that executes an action whenever the value read from the queue is 10:

import threading
import time
import queue

def foo():
    q = queue.Queue()

    def ticker():
        while True:
            for i in range(1,11):
                print(f'Timer is {i}')
                q.put(i)
                time.sleep(1)

    t_ticker = threading.Thread(target=ticker)
    t_ticker.start()

    while True:
        i = q.get()
        if i == 10:
            print("Action is executed")        

foo()

2 Comments

But there are while loops in this code. They consume valuable cpu time. I have much more things for cpu to do. Is there a way to do it without using infinite loops.
"They consume valuable cpu time." They do not, because of the semantics of the queue get and put methods. Calling q.get() blocks if there are no items available in the queue. Please take a look at the relevant docs (and of course the other loop is only excuting 1/second, so again it is not "consuming valuable CPU time").

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.