I need to use unbuffered output in a script that is built with buildout.
My approach was to specify the -u
flag for Python in the generated script.
Here's my buildout.cfg:
[buildout]
parts = python
develop = .
[python]
recipe = zc.recipe.egg:scripts
eggs = myproject
And setup.py:
from setuptools import setup, find_packages
setup(
name = 'myproject',
packages = find_packages(),
entry_points = """
[console_scripts]
myscript = myproject:main
""",
)
I get the following shebang with this configuration:
$ pip install .
$ head -n1 /usr/local/bin/myscript
#!/usr/bin/python
And I want this:
#!/usr/bin/python -u
How to do it? I tried adding arguments = -u
and interpreter = python -u
to buildout.cfg
. It didn't work.
You can force unbuffered I/O from within your Python script by re-opening stdin or stdout by opening a new file object on the filenumber:
import io, os, sys
try:
# Python 3, open as binary, then wrap in a TextIOWrapper
unbuffered = io.TextIOWrapper(open(sys.stdout.fileno(), 'wb', 0), write_through=True)
except TypeError:
# Python 2
unbuffered = os.fdopen(sys.stdout.fileno(), 'w', 0)
You can then reassign sys.stdout if you want to use other modules or build-ins that use stdout or stdin:
sys.stdout = unbuffered
Also see unbuffered stdout in python (as in python -u) from within the program