[BUG] Progress(): Terminal size mis-detected if run in multiprocessing sub-process
See original GitHub issueDescribe the bug
Using multiprocessing
to run a sub process, initializing Progress() under that process fails to detect the terminal size, using the fallback (80, 25) instead.
To Reproduce Sample code:
from multiprocessing import Process, Queue
from rich.console import Console
from rich.progress import Progress
def sub_process():
pre_console = Console()
print(f"Sub process PRE console size: {pre_console.size}\n")
with Progress() as progress:
print(f"Sub process progress.console.size: {progress.console.size}")
console = Console()
print(f"Sub process console size: {console.size}\n")
if __name__ == "__main__":
with Progress() as main_progress:
print(f"Main progress.console.size: {main_progress.console.size}")
main_console = Console()
print(f"Main console size: {main_console.size}\n")
q = Queue()
process = Process(target=sub_process)
process.start()
process.join()
Output is:
Main progress.console.size: ConsoleDimensions(width=151, height=45)
Main console size: ConsoleDimensions(width=151, height=45)
Sub process PRE console size: ConsoleDimensions(width=151, height=45)
Sub process progress.console.size: ConsoleDimensions(width=80, height=25)
Sub process console size: ConsoleDimensions(width=80, height=25)
-> Note the difference between the 3 variants:
- instances in main module (correct terminal size)
- instance in sub process, before Progress() is called (correct)
- instances in sub process, after Progress() is called (fallback)
Platform Macos 11.4 (Big Sur) iTerm 2 python 3.9.4
Diagnose I may ask you to cut and paste the output of the following commands. It may save some time if you do it now.
rich.diagnose
โญโโโโโโโโโโโโโโโโโโโโโโโโโ <class 'rich.console.Console'> โโโโโโโโโโโโโโโโโโโโโโโโโโฎ
โ A high level console interface. โ
โ โ
โ โญโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฎ โ
โ โ <console width=151 ColorSystem.TRUECOLOR> โ โ
โ โฐโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฏ โ
โ โ
โ color_system = 'truecolor' โ
โ encoding = 'utf-8' โ
โ file = <_io.TextIOWrapper name='<stdout>' mode='w' encoding='utf-8'> โ
โ height = 45 โ
โ is_alt_screen = False โ
โ is_dumb_terminal = False โ
โ is_interactive = True โ
โ is_jupyter = False โ
โ is_terminal = True โ
โ legacy_windows = False โ
โ no_color = False โ
โ options = ConsoleOptions( โ
โ size=ConsoleDimensions(width=151, height=45), โ
โ legacy_windows=False, โ
โ min_width=1, โ
โ max_width=151, โ
โ is_terminal=True, โ
โ encoding='utf-8', โ
โ justify=None, โ
โ overflow=None, โ
โ no_wrap=False, โ
โ highlight=None, โ
โ markup=None, โ
โ height=None โ
โ ) โ
โ quiet = False โ
โ record = False โ
โ safe_box = True โ
โ size = ConsoleDimensions(width=151, height=45) โ
โ soft_wrap = False โ
โ stderr = False โ
โ style = None โ
โ tab_size = 8 โ
โ width = 151 โ
โฐโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฏ
rich._windows
platform="Darwin"
WindowsConsoleFeatures(vt=False, truecolor=False)
pip freeze | grep rich
rich==10.2.2
** Workaround ** Initalize a Console() with width and height from shutil.get_terminal_size(), then initialize Progress() with that console:
terminal_size: os.terminal_size = shutil.get_terminal_size()
console = Console(width=terminal_size.columns, height=terminal_size.lines)
progress = Progress(console=console)
It would seem that Console() only uses shutil.get_terminal_size() under Windows. Otherwise it uses os.get_terminal_size() for stdin or stdout - but I guess (I havenโt fully explored your code, sorry) that the redirects that are enabled by Progress() / Live(), coupled with the multiprocessing, somehow mess that up. Perhaps shutil could be tried first, regardless of platform?
Thanks for an awesome library!
Issue Analytics
- State:
- Created 2 years ago
- Comments:5
Please try 10.6.0 It will use sys.stdin etc. so it should still work even when wrapper.
I ran into the same issue and the new release appears to have solved it. Thanks a lot!