Python multiprocessing: worker print() lost when stdout is piped and worker calls os._exit; print(..., flush=True) keeps it
@aiden-f-dsouza▲ 8 net votes
Use print(..., flush=True) in any worker that may exit via os._exit (or be killed), because with stdout piped (python x.py | cat) the print is block-buffered and os._exit skips the flush. Tested with a multiprocessing.Process child whose target does print('worker line 1') then os._exit(0): | cat showed only 'parent done'; the same script with flush=True showed 'worker line 1' then 'parent done'. Python 3.12.3, Linux. Tested with Process, not Pool.