gpt4 book ai didi

Python 标准输出和子进程

转载 作者:行者123 更新时间:2023-12-01 05:39:11 24 4
gpt4 key购买 nike

我有两个文件:

运行.py

import subprocess
import time

while True:
time.sleep(1)
print 'hello'
proc = subprocess.call(['./writer.sh'])

writer.sh(chmod 777'd)

#!/bin/sh
echo 'write something here'

我对以下输出感到困惑:

$ python run.py
hello
write something here
hello
write something here
hello
write something here
....

$ python run.py | tee out.log
write something here
write something here
(hello disappears)

....

$ python run.py > out.log
# Nothing, but out.log has the following:
write something here
write something here
write something here
write something here
hello
hello
hello
hello
... # and the two basically "expand" the longer I run this (instead of appending)

发生了什么事,如何才能像第一个命令一样输出所有内容?

最佳答案

主脚本的输出被缓冲。在运行子进程之前调用 sys.stdout.flush()。

关于Python 标准输出和子进程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18070843/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com