Skip to content

Latest commit

 

History

History
29 lines (22 loc) · 638 Bytes

Unbuffered Python.md

File metadata and controls

29 lines (22 loc) · 638 Bytes
title created modified
Unbuffered Python
2024-01-06T14:33:52.573Z
2024-01-06T14:36:43.611Z

Unbuffered Python

When reading bytes asynchronously from stdout using asyncio.create_subprocess_exec, the program has to be unbuffered.

python3 -u <script_path>
# this can only make `print` into unbuffered
import builtins
import copy

old_print = copy.copy(builtins.print)

def custom_print(*args, **kwargs):
    if 'flush' not in kwargs:
        kwargs['flush'] = True
    old_print(*args, **kwargs)

# Override the built-in print function with the custom function
builtins.print = custom_print