我在python中做print语句.我正在执行我的脚本:
python script.py > out.log nohup &
Run Code Online (Sandbox Code Playgroud)
打印报表并未全部显示,out.log但程序正在完成.
那行代码在.sh我执行的文件中./script.sh
更新:日志确实获取所有数据,但直到打印出一定数量的行.它似乎是在缓冲输出.
如果我使用这样的命令:
./ program >> a.txt&
,并且程序是一个长时间运行的命令,那么我只能在程序结束后看到输出.这意味着我无法知道计算是否进展顺利,直到实际停止计算.我希望能够在程序运行时读取文件上的重定向输出.
这类似于打开文件,附加到文件,然后在每次写入后关闭它.如果文件仅在程序结束时关闭,则在程序结束之前不能读取任何数据.我所知道的唯一重定向类似于在程序结束时关闭文件.
你可以用这个小python脚本测试它.语言并不重要.任何写入标准输出的程序都有同样的问题.
l = range(0,100000)
for i in l:
if i%1000==0:
print i
for j in l:
s = i + j
Run Code Online (Sandbox Code Playgroud)
可以使用以下
命令运行:./ python program.py >> a.txt
然后cat a.txt ..一旦脚本完成计算,您将只获得结果.
这是麻省理工学院python项目的一个问题,但它基本上是为python 2.x用户编写的,那么有没有办法修复下面的代码来运行最新的python 3?
当前代码提出"ValueError:不能有无缓冲的文本I/O"
WORDLIST_FILENAME = "words.txt"
def load_words():
print("Loading word list from file...")
inFile = open(WORDLIST_FILENAME, 'r', 0)
# wordlist: list of strings
wordlist = []
for line in inFile:
wordlist.append(line.strip().lower())
print(" ", len(wordlist), "words loaded.")
return wordlist
Run Code Online (Sandbox Code Playgroud) 默认情况下,Flask会直接使用INFO标记记录GET和POST请求等内容.实现自定义记录器时,这些记录器将发布到同一记录器并使我的INFO层变得混乱.有没有办法将它们降级到像DEBUG这样的另一层?
这是我使用的记录器:
# create logger
FORMAT = '%(asctime)s - %(module)s - %(levelname)s - Thread_name: %(threadName)s - %(message)s'
logging.basicConfig(
format=FORMAT, datefmt='%m/%d/%Y %I:%M:%S %p',
filename='wizard/logs/example.log', level=logging.DEBUG)
Run Code Online (Sandbox Code Playgroud) 可能重复:
如何刷新Python打印输出?
python中的无缓冲的stdout(如在python -u中)来自程序内部
我有以下代码来刷新输出缓冲区.
print 'return 1'
sys.stdout.flush()
Run Code Online (Sandbox Code Playgroud)
我可以设置打印功能,以便在调用时自动刷新缓冲区吗?
所以我一直在尝试使用numpy和matplotlib,并且在从emacs劣质shell运行python时遇到了一些错误.
当我将py文件发送到shell解释器时,我可以在代码执行后运行命令.命令提示符">>>"显示正常.但是,在我在一个绘图上调用matplotlib show命令之后,shell就会挂起,而命令提示符没有显示.
>>> plt.plot(x,u_k[1,:]);
[<matplotlib.lines.Line2D object at 0x0000000004A9A358>]
>>> plt.show();
Run Code Online (Sandbox Code Playgroud)
我正在运行传统的C-python实现.在emacs 23.3下使用Fabian Gallina的Python python.el v.0.23.1在Win7上.
在i-python平台下提出了一个类似的问题:在windows上的emacs里面的py-shell上运行matplotlib或enthought.mayavi.mlab
更新:我在Win 7 x64的新安装上重复了这个问题,python网站上提供了典型的python 2.7.2二进制文件,在emacs 23.3和23.4上用于Windows的numpy 1.6.1和matplotlib 1.1.0.
emacs shell中某处必定存在错误.
Docker 映像大小通常应尽可能小。使用像标准 python映像这样的成熟环境,在安装了所有依赖项的情况下,经常会导致严重臃肿的映像。将 python 打包成独立的可执行文件(例如使用 pyinstaller)是减少图像大小和整体复杂性的完美方式。
环境:python3.6,pyinstaller==3.4
出现的问题是,python 使用每个默认的缓冲 stdio。这可以通过使用python -u .... 但是在使用 pyinstaller 时变得无法访问。
根据文档,应该可以向生成的可执行文件添加运行时选项,例如u,v和W ...。但不幸的是,实际上它似乎不起作用。两者,v并且W,正常工作,但u似乎被完全忽略。
以下代码段显示了用法:
...
exe = EXE(...
[('u', None, 'OPTION')],
name="myapp",
...)
...
Run Code Online (Sandbox Code Playgroud)
这个标志还有效吗?由于其他人工作 - 它是否在没有通知或更新文档的情况下被删除?
是否有替代方法可以禁用 stdio 缓冲(使用 pyinstaller 或外部),而不像这样修改 python 代码?
在运行 docker swarm 服务时,应避免缓冲 IO。为了使可执行文件正确地实时登录到 docker 守护进程,有必要附加一个 shell。但是将 tty shell 附加到 swarm 任务会使处理日志变得更加复杂,甚至是不可能的。
如果将尾随逗号添加到print语句的末尾,则首先执行下一个语句.为什么是这样?例如,10000 ** 10000它在打印之前执行"Hi ":
print "Hi",
print 10000 ** 10000
Run Code Online (Sandbox Code Playgroud)
打印"嗨你好"之前需要一段时间:
def sayHello():
for i in [0] * 100000000: pass
print "Hello"
print "Hi",
sayHello()
Run Code Online (Sandbox Code Playgroud) 我正在使用这样的东西:
find folder/ | xargs -n1 -P10 ./logger.py > collab
Run Code Online (Sandbox Code Playgroud)
里面logger.py我处理出来的文件输出格式化线.合作应该是这样的
{'filename' : 'file1', 'size' : 1000}
{'filename' : 'file1', 'size' : 1000}
{'filename' : 'file1', 'size' : 1000}
{'filename' : 'file1', 'size' : 1000}
Run Code Online (Sandbox Code Playgroud)
相反,有时线条变得混乱:
{'filename' : 'file1', 'size' : 1000}
{'file
{'filename' : 'file1', 'size' : 1000}
name' : 'file1', 'size' : 1000}
{'filename' : 'file1', 'size' : 1000}
Run Code Online (Sandbox Code Playgroud)
我该如何预防/纠正这个?
也许在以太中有人可以帮我解决这个问题.(我在SO上已经看到了很多类似的问题,但没有一个涉及标准输出和标准错误或处理与我相似的情况,因此这个新问题.)
我有一个python函数打开一个子进程,等待它完成,然后输出返回代码,以及标准输出和标准错误管道的内容.当进程正在运行时,我还想在填充它们时显示两个管道的输出.我的第一次尝试导致了这样的事情:
process = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout = str()
stderr = str()
returnCode = None
while True:
# collect return code and pipe info
stdoutPiece = process.stdout.read()
stdout = stdout + stdoutPiece
stderrPiece = process.stderr.read()
stderr = stderr + stderrPiece
returnCode = process.poll()
# check for the end of pipes and return code
if stdoutPiece == '' and stderrPiece == '' and returnCode != None:
return returnCode, stdout, stderr
if stdoutPiece != '': print(stdoutPiece)
if stderrPiece != '': print(stderrPiece) …Run Code Online (Sandbox Code Playgroud) python ×9
bash ×2
linux ×2
docker ×1
emacs ×1
flask ×1
flush ×1
logging ×1
matplotlib ×1
pyinstaller ×1
python-3.x ×1
stdio ×1
subprocess ×1
text ×1
xargs ×1