Thanks to visit codestin.com
Credit goes to github.com

Skip to content

tqdm + logging, message containing line feed gets eaten after line feed #724

@Erawpalassalg

Description

@Erawpalassalg

tqdm v. 4.31.1
Python v. 3.7.0
Linux version : Debian stretch (this bug does not reproduce on MacOs or ArchLinux)

When using tqdm with logging, configured as @casperdcl suggests it in #313 BUT with a line feed in the message, tqdm eats the new line.

Test code:

import tqdm
import logging
import time


class TqdmStream(object):
  @classmethod
  def write(_, msg):
    tqdm.tqdm.write(msg, end='')

  # is this required?
  # @classmethod
  # def flush(_):
  #   pass


if __name__ == "__main__":
  logging.basicConfig(level=logging.DEBUG, stream=TqdmStream)

  log = logging.getLogger(__name__)
  log.info("loop")
  # for x in tqdm.trange(1, 101, mininterval=10):
  for x in tqdm.trange(1, 101):
    time.sleep(.2)
    if not x % 20:
      log.debug("in loop %d\nnew line" % x)
  log.info("done")

Now, several test cases:

  • If a \n is added to the end of the message, the text now gets displayed properly (with the line feed at the end, but we asked for it)
  • If we use end="\n" instead of end="", the message gets displayed properly but with two line feeds at the end.
  • passing end="test" shows that tqdm seems to have an issue with stdout buffering, since it appends "test" to the next logging line (probably an issue on itself, not sure how it's related to this issue)

One might think that this is a flushing issue, but adding the following solves the 3rd test case (while breaking the progress bar though), while doing nothing for the 2nd one (not even breaking the progress bar).

import sys

class TqdmStream(object):
  @classmethod
  def write(_, msg):
    tqdm.tqdm.write(msg, end='')

  @classmethod
  def flush(_):
    sys.sdout.flush()

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions