Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use tqdm in Kubernetes

I'm using Kubernetes, and a training job runs on the cluster. I'm using TQDM as progress bar, but unlike what I've expected, the progress bar doesn't show up when I check Kubernetes Pod logs. Does anyone have a solution to this problem?

like image 367
Piljae Chae Avatar asked Sep 14 '20 09:09

Piljae Chae


People also ask

How do I use tqdm in Python?

Usage. Using tqdm is very simple, you just need to add your code between tqdm() after importing the library in your code. You need to make sure that the code you put in between the tqdm() function must be iterable or it would not work at all.

How do you define tqdm?

tqdm derives from the Arabic word taqaddum (تقدّم) which can mean "progress," and is an abbreviation for "I love you so much" in Spanish (te quiero demasiado). Instantly make your loops show a smart progress meter - just wrap any iterable with tqdm(iterable) , and you're done!

Does tqdm work in terminal?

tqdm 's command line interface (CLI) can be used in a script or on the terminal/console.

What is tqdm Python package?

tqdm is a Python library that allows you to output a smart progress bar by wrapping around any iterable. A tqdm progress bar not only shows you how much time has elapsed, but also shows the estimated time remaining for the iterable.

What is tqdm in Python?

Tqdm is a Python library used to display smart progress bars that show the progress of your Python code execution. This library can also be used to see the progress of a machine learning model while training the model on a very large data set. Sometimes it takes a lot of time while training a machine learning model on a very huge dataset.

How to use tqdm in Jupyter?

The tqdm Python library helps make progress explicit. The tqdm module works with the console, but it also has special support for one of my favorite environments: Jupyter. To use tqdm in Jupyter, you need to import the notebook submodule and have ipywidgets installed. The notebook submodule is interface-compatible with tqdm.

What are the different parameters in a tqdm?

There are multiple parameters in a tqdm; let us understand them one by one. Iterable – It can be a range, a list whose progress we have to check. The above output shows that a total of 200 iterations took place at a speed of 94.85 iterations per second. The total time was approx. 2 seconds.

What is a Kubernetes tutorial?

Tutorials This section of the Kubernetes documentation contains tutorials. A tutorial shows how to accomplish a goal that is larger than a single task. Typically a tutorial has several sections, each of which has a sequence of steps.


2 Answers

I don't have a good solution, but this one helped me:

tqdm progress bar will show as expected if you use:

kubectl attach <pod_name>

instead of:

kubectl logs <pod_name>
like image 131
GalDude33 Avatar answered Oct 18 '22 02:10

GalDude33


So far unfortunately I haven't found a satisfying answer to this either. Depending on whether you use kubectl or k9s for example or even how the iterable looks like inside tqdm, or what else you log via for example the logging module, the results can be wildly different from no progress bar shown at all, to sporadic flushes of dozens of iterations. The only way I have found to get consistent kubernetes logs is printing them yourself every iteration and redirecting tqdm output to somewhere else.

For example as follows:

import time
import sys
from tqdm import tqdm

iterator = tqdm(range(99), file=open("/dev/null", "w"))

for x in iterator:
    print(iterator.__str__())
    sys.stdout.flush()
    time.sleep(0.2)
like image 1
RagingPixels Avatar answered Oct 18 '22 03:10

RagingPixels