Skip to content

Instantly share code, notes, and snippets.

@jonathanking
Last active February 15, 2023 21:34
Show Gist options
  • Save jonathanking/aae1547fc2cba6a300786610a4e844df to your computer and use it in GitHub Desktop.
Save jonathanking/aae1547fc2cba6a300786610a4e844df to your computer and use it in GitHub Desktop.
Modifying TQDM to avoid carriage returns in SLURM job output
import os
from tqdm import tqdm
from tqdm.utils import _unicode, disp_len
_TQDM_STATUS_EVERY_N = 2
if "SLURM_JOB_ID" in os.environ:
def status_printer(self, file):
"""
Manage the printing and in-place updating of a line of characters.
Note that if the string is longer than a line, then in-place
updating may not work (it will print a new line at each refresh).
"""
self._status_printer_counter = 0
fp = file
fp_flush = getattr(fp, 'flush', lambda: None) # pragma: no cover
if fp in (sys.stderr, sys.stdout):
getattr(sys.stderr, 'flush', lambda: None)()
getattr(sys.stdout, 'flush', lambda: None)()
def fp_write(s):
fp.write(_unicode(s))
fp_flush()
last_len = [0]
def print_status(s):
self._status_printer_counter += 1
if self._status_printer_counter % _TQDM_STATUS_EVERY_N == 0:
len_s = disp_len(s)
# This is where we've removed the \r for clearer output
fp_write(s + (' ' * max(last_len[0] - len_s, 0)) + '\n')
last_len[0] = len_s
return print_status
tqdm.status_printer = status_printer
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment