How to pause and resume loop timing? #1560
Unanswered
LouisDesdoigts
asked this question in
Q&A
Replies: 1 comment
-
I don't know if a built-in feature exists for directly pausing and resuming the progress bar. However, you can achieve a similar effect by using for example see this code: from tqdm import tqdm
import time
def main_fn():
time.sleep(0.1) # Simulate some time-consuming operation
return
def expensive_fn():
# Do things
time.sleep(5)
return
bar = tqdm(total=100)
pause_flag = False
for i in range(100):
# We want to count the main function time
main_fn()
# We dont want this calculation to contribute to the loop timer
if (i + 1) % 10 == 0:
bar.update(0) # Update progress bar by 0 (just we paused it)
pause_flag = True
expensive_fn()
else:
bar.update(1) # Update progress bar normally
if pause_flag:
# Update progress bar again because we paused it, if we didn't do it again it will not reach the 100%
bar.update(1)
pause_flag = False
bar.close() # Close the progress bar when finished I'm not sure if this will be helpful, but I'm happy to share it with you anyway. Let's hope it can be of some use! 😄 |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
So apologies if this has already been asked, but is there a way to pause and un-pause the timing of the loops?
My problem is I have a simple loop, where every nth iteration I want to do an expensive calculation that doesn't contribute to the loop time. Example:
Does something like this exist already, or would this be a new feature request. Also any suggestions of how to implement this are very welcome!
Beta Was this translation helpful? Give feedback.
All reactions