You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hello, I'm not sure I'm facing a visual output bug or if I'm doing something wrong when combining tqdm, multiprocessing and logging.
In the reproducer below, when I set VERBOSITY = 0, the log() calls from the worker processes output nothing and the progress bar is displayed correctly.
However, as soon as I log from the worker processes by setting VERBOSITY = 1, then here's what happening
While the progress bar is properly positioned and sticks to the bottom of the screen
Most of the time, tqdm displays a progress of 0% and only occasionally it flashes with the proper progress and percentage
At the very end, the last update outputs the proper progress: Processing...: 100%|██████████| 1000/1000 [00:31<00:00, 31.67foo/s] however it doesn't take the full width of the screen
import itertools
import logging
import multiprocessing
import random
import sys
import time
import tqdm
from tqdm.contrib import DummyTqdmFile
log: logging.Logger = logging.getLogger(__name__)
DEFAULT_FORMAT = "[%(asctime)s.%(msecs)06d][%(processName)s][%(threadName)s][%(levelname)s][%(module)s] %(message)s"
VERBOSITY = 1
def configure_logger(log: logging.Logger, verbosity: int, *, format: str = DEFAULT_FORMAT, dry_run: bool = False):
"""Configures the logger instance based on verbosity level"""
# can't use force=true as it requires Python >= 3.8
root = logging.getLogger()
for handler in root.handlers:
root.removeHandler(handler)
if dry_run:
format = format.replace(" %(message)s", "[DRY RUN] %(message)s")
logging.basicConfig(format=format, datefmt="%Y-%m-%d %H:%M:%S", stream=DummyTqdmFile(sys.stdout))
verbosity_to_loglevel = {0: logging.WARNING, 1: logging.INFO, 2: logging.DEBUG}
log.setLevel(verbosity_to_loglevel[min(verbosity, 2)])
if verbosity >= 4:
logging.getLogger().setLevel(logging.INFO)
if verbosity >= 5:
logging.getLogger().setLevel(logging.DEBUG)
def by_n(iterable, n):
"""Iterate by chunks of n items"""
return (tuple(filter(lambda x: x is not None, x)) for x in itertools.zip_longest(*[iter(iterable)] * n))
def _worker(batch):
log.info("Let's go!")
for i in batch:
log.info("Processing item %d", i)
time.sleep(random.uniform(0.1, 0.2))
log.info("Done!")
return len(batch)
if __name__ == "__main__":
configure_logger(log, VERBOSITY)
log.info("Let's go!")
foos = list(range(1000))
kwargs = {
"desc": "Processing...",
"total": len(foos),
"leave": True,
"mininterval": 1,
"maxinterval": 5,
"unit": "foo",
"dynamic_ncols": True,
}
pbar = tqdm.tqdm(**kwargs)
with multiprocessing.Pool(processes=5) as workers_pool:
for batch_length in workers_pool.imap_unordered(
_worker,
by_n(foos, min(10 + int((len(foos) - 10) / 100), 1000)),
):
pbar.update(batch_length)
log.info("Done!")
Here is a screenshot while the script is running
And here is a screenshot at the very end after it has terminated
The text was updated successfully, but these errors were encountered:
read the known issues
environment, where applicable:
Hello, I'm not sure I'm facing a visual output bug or if I'm doing something wrong when combining
tqdm
,multiprocessing
andlogging
.In the reproducer below, when I set
VERBOSITY = 0
, thelog()
calls from the worker processes output nothing and the progress bar is displayed correctly.However, as soon as I log from the worker processes by setting
VERBOSITY = 1
, then here's what happeningtqdm
displays a progress of0%
and only occasionally it flashes with the proper progress and percentageProcessing...: 100%|██████████| 1000/1000 [00:31<00:00, 31.67foo/s]
however it doesn't take the full width of the screenHere is a screenshot while the script is running
And here is a screenshot at the very end after it has terminated
The text was updated successfully, but these errors were encountered: