1

I have a job in Kubernetes with a python script that is printing a tqdm progress bar to stdout (or stderr?). However, the progress bar cannot be seen with:

kubectl logs test-qpgb7 # returns nothing 
kubectl logs -f test-qpgb7 # hangs and returns nothing also
apiVersion: batch/v1
kind: Job
metadata:
  name: test
  labels:
    job: test
spec:
  template:
    spec:
      containers:
        - name: test
          image: my-image
          imagePullPolicy: IfNotPresent
          command: ["python", "test.py"]
      restartPolicy: Never
  backoffLimit: 1 
# test.py
import tqdm
from time import sleep

if __name__ == "__main__":
    for i in tqdm.tqdm(range(0, 1000)):
        sleep(2)

Is it possible to see the progress bar status while the job has not yet completed? When the job completes kubectl logs test-qpgb7 does work.

UPDATE: The problem does not seem to be in tqdm since this does not show output either:

# test.py
# import tqdm
from time import sleep

if __name__ == "__main__":
    for i in range(0, 1000):
        print(str(i))
        sleep(2)
FROM python:3.9.1 

WORKDIR /integrity 

COPY requirements.txt . 

COPY src/ . 

RUN pip install -r requirements.txt 

UPDATE2: adding a sys.stdout.flush() in the print for loop:

if __name__ == "__main__":
    for i in range(0, 1000):
        print(str(i))
        sys.stdout.flush()
        sleep(2)

Does work with just the print but not with the tqdm progress. Note sys.stderr.flush() was also added with no success.

UPDATE3: For now a temporary solution is:

from tqdm import tqdm
from time import sleep
import sys

if __name__ == "__main__":
    for i in tqdm(range(0, 1000), file=sys.stdout):
        print("")
        sys.stdout.flush()
        sys.stderr.flush()
        sleep(2)
# kubectl logs -f  test007-ctqjh
  0%|          | 0/1000 [00:00<?, ?it/s]
  0%|          | 1/1000 [00:02<33:20,  2.00s/it]
  0%|          | 2/1000 [00:04<33:18,  2.00s/it]
  0%|          | 3/1000 [00:06<33:16,  2.00s/it]
  0%|          | 4/1000 [00:08<33:14,  2.00s/it]
  0%|          | 5/1000 [00:10<33:12,  2.00s/it]
  1%|          | 6/1000 [00:12<33:10,  2.00s/it]

which prints on a new line every time. However, will be interested if someone finds how to make this work without printing to a new line.

John Karasev
  • 151
  • 2
  • 11

0 Answers0