简体   繁体   中英

How to show status in a very slow loop in python

I want to show status every second in a very slow loop in python code, eg

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, eg

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

and the output updates every second, eg

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

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: 在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM