Search code examples
pythonpython-3.xtimer

Timer to perform task in Python --CLOSED--


I know this question is not that good but I am kind of stuck here and I've been searching about timers but the answers are just not what I am looking for.

Okay so I have a for loop here and this for loop is trying to do something continuously but I need it to do its task for a minute that I have chosen and let's say 3 minutes , so it would be like

for i in sample_iteration:
   #Tasks are being done here

But I need those tasks inside the for loop to keep going for 3 minutes and I have found this timer from geeks for geeks and this is the code

# import the time module 
import time 

# define the countdown func. 
def countdown(t): 
    
    while t: 
        mins, secs = divmod(t, 60) 
        timer = '{:02d}:{:02d}'.format(mins, secs) 
        print(timer, end="\r") 
        time.sleep(1) 
        t -= 1
    
    print('Fire in the hole!!') 


# input time in seconds 
t = input("Enter the time in seconds: ") 

# function call 
countdown(int(t)) 

Now I tried this one right here, now it does the countdown for 3 minutes but it also does the time.sleep(1) for one second and I can't have it paused for every second cause I need to to keep going as possible.

t = 180 #seconds
for i in sample_iteration:
    mins, secs = divmod(t, 60) 
    timer = '{:02d}:{:02d}'.format(mins, secs) 
    print(timer, end="\r") 
    time.sleep(1) 
    t -= 1
    
    #Do the task here

I can easily I think make a condition that when t is 0 I could break the loop

Now long ago I had this java program that does the work and involves some nano time with I understand this and the counting is based on nano time cause 100000000 nanoseconds is equal to 1 second right? (correct me if I am wrong) so yeah and here is the code

public class Timer {
    
       public static void main(String[] args){
       
        long lastTime = System.nanoTime();
        boolean running = true;
        int timer = 0;
        long now ;
       int i = 0;
        while(running)   
        {
            now = System.nanoTime();
            timer += (now - lastTime);
            lastTime = now;
            
            if(timer >= 1000000000)
            {
             i++;
              timer = 0; 
              System.out.println(i);
            }
        }
        
       }
       
}

I was thinking of doing the same with python but I'm getting lost to be honest and I am just a beginner and tried to get help here in this great community! Also maybe there's an alternative where I do not have to apply based on the java program cause maybe there's a better way and shorter way with python. Thank you! Looking forward for this THANK YOU!


Solution

  • ---UPDATE---

    Hello I managed to figure it out! Thanks for the answers this is what I've done

    i = 0
    lastTime = time.time_ns()
    timer = 0
    
    for i in sample_iteration:
        now = time.time_ns()
        timer += (now - lastTime)
        lastTime = now
    
        if timer >= 1000000000:
            i+=1
            timer = 0
            print(i)
    

    I used the time.time_ns() to get the nanoseconds cause time.time() is different vs time.time_ns(). time.time() is getting the time in seconds since the epoch and it is not in nano seconds which is what I was looking for

    time.time_ns() method of Time module is used to get the time in nanoseconds since the epoch. To get the time in seconds since the epoch, we can use time.time() method.

    The epoch is the point where the time starts and is platform dependent. On Windows and most Unix systems, the epoch is January 1, 1970, 00:00:00 (UTC) and leap seconds are not counted towards the time in seconds since the epoch. To check what the epoch is on a given platform we can use time.gmtime(0).

    Source: Geeks for Geeks

    Thank you stack overflow community!