gpt4 book ai didi

python - sys.stderr在Python3中是 block 设备还是字符设备?

转载 作者:太空狗 更新时间:2023-10-29 11:46:04 25 4
gpt4 key购买 nike

在C语言中,如果我这样写代码:

#include <stdio.h>
#include <unistd.h>
int main()
{
while(1)
{
fprintf(stdout,"hello-std-out");
fprintf(stderr,"hello-std-err");
sleep(1);
}
return 0;
}

stdout 不会显示,因为它是 block 设备。但是会显示 stderr,因为它不是。

但是,如果我在 Python3 中编写类似的代码:

import sys
import time
if __name__ == '__main__':
while True:
sys.stdout.write("hello-std-out")
sys.stderr.write("hello-stderr")
time.sleep(1)

如果我不刷新这些缓冲区,stdout 和 stderr 都不会显示。这是否意味着 sys.stderr 也是 Python 中的 block 设备?

最佳答案

如果您没有看到 stderr,那么您使用的是 Python3,其中文本 IO 层在连接到 tty 时是行缓冲的,否则是 block 缓冲的,无论 -u 选项如何。

缓冲问题与字符/ block 设备无关。

关于python - sys.stderr在Python3中是 block 设备还是字符设备?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14249888/

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