强制使用Buildout和zc.Receipe.egg生成的脚本的无缓冲输出:脚本 [英] Force unbuffered output for script made with buildout and zc.recipe.egg:scripts

查看:22
本文介绍了强制使用Buildout和zc.Receipe.egg生成的脚本的无缓冲输出:脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在使用Buildout生成的脚本中使用无缓冲输出。

我的方法是在生成的脚本中为Python指定-u标志。

这是我的Buildout.cfg:

[buildout]
parts = python
develop = .

[python]
recipe = zc.recipe.egg:scripts
eggs = myproject

和setup.py:

from setuptools import setup, find_packages

setup(
    name = 'myproject',
    packages = find_packages(),
    entry_points = """
    [console_scripts]
    myscript = myproject:main
    """,
)

我使用此配置得到以下Shebang:

$ pip install .
$ head -n1 /usr/local/bin/myscript
#!/usr/bin/python

我想要这个:

#!/usr/bin/python -u

怎么做?我尝试将arguments = -uinterpreter = python -u添加到buildout.cfg。它没有起作用。

推荐答案

您可以通过在文件编号上打开新的文件对象来重新打开标准输入或标准输出,从而从您的PYTHON脚本中强制执行未缓冲的I/O:

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)

如果要使用使用标准输出或标准输入的其他模块或内置模块,则可以重新分配sys.stdout:

sys.stdout = unbuffered

另见unbuffered stdout in python (as in python -u) from within the program

这篇关于强制使用Buildout和zc.Receipe.egg生成的脚本的无缓冲输出:脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆