2

I want to show status every second in a very slow loop in python code, e.g.

for i in range(100):
    sleep(1000000) # think there is a very slow job

    # I want to show status in console every second
    # to know if the job stop or not

The output image is, e.g.

$ python somejob.py
> 2022-09-02 13:04:10 | Status: running... 

and the output updates every second, e.g.

$ python somejob.py
> 2022-09-02 13:04:11 | Status: running... 
$ python somejob.py
> 2022-09-02 13:04:12 | Status: running... 
$ python somejob.py
> 2022-09-02 13:04:13 | Status: running... 

Any idea will by helpful. Thx!!!

  • progress bar is even better idea. Check this https://stackoverflow.com/questions/43259717/progress-bar-for-a-for-loop-in-python-script – Gedas Miksenas Sep 02 '22 at 10:43

2 Answers2

1

I think what you're looking for is someting like the tqdm library: github repo

for example

from tqdm import tqdm
for i in tqdm(range(1000)):
    continue # do something complex here
0

You may us the rich module to disply a progress bar:

import time
from rich.progress import track

for i in track(range(100)):
    time.sleep(0.5)

Here's a screenshot within the run: enter image description here

MagnusO_O
  • 1,202
  • 4
  • 13
  • 19