Skip to content Skip to sidebar Skip to footer

Does Time.sleep() Stop All Executions?

In my complex python program, when it's running, I have a piece of code that executes every 3 seconds that prints the program's progress as the percentage of the execution that's f

Solution 1:

Yes, time.sleep will halt your program.

Use time.time in your loop and check when three seconds have passed.

Solution 2:

time.sleep(seconds) will stop execution on the current thread. Therefore, it will completely stop your program on that thread: nothing else will happen until those seconds pass.

You don't have to worry about this. If the program uses threading, then the other threads shouldn't halt.

Solution 3:

from time import time

prev = time()
while True:
    now = time()
    if now - prev > 3:
        print'report'
        prev = now
    else:
        pass
        # runs

Solution 4:

The proper way to do this is with signal

import signal

defhandler(signum, frame):
    print i
    if i>100000000:
        raise Exception("the end")
    else:
        signal.alarm(3)      

signal.signal(signal.SIGALRM, handler)   
signal.alarm(3)     

i=0whileTrue:
   i+=1

Post a Comment for "Does Time.sleep() Stop All Executions?"