How to show status in a very slow loop in python

Viewed 45

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!!!

2 Answers

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

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

Related