V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX  ›  OhYee  ›  全部回复第 11 页 / 共 15 页
回复总数  292
1 ... 3  4  5  6  7  8  9  10  11  12 ... 15  
2019-01-21 18:32:47 +08:00
回复了 niuoh 创建的主题 全球工单系统 希望微信可以支持匿名发朋友圈
qq 空间秘密——撕逼战场

这是准备要匿名骂亲戚么
2019-01-15 16:20:28 +08:00
回复了 OhYee 创建的主题 问与答 为什么无缓冲的 stderr 会比有缓冲的 stdout 输出的还晚?
@swulling
感谢,试了下 python3.6.7 没有问题,python3.7.1 存在这个问题
看了下两者的区别,应该就是 unbuffered 导致被截断掉了

此贴完结,感谢几位大佬的帮助

————————————

Python3.6

https://docs.python.org/3.6/using/cmdline.html#cmdoption-u
-u
Force the binary layer of the stdout and stderr streams (which is available as their buffer attribute) to be unbuffered. The text I/O layer will still be line-buffered if writing to the console, or block-buffered if redirected to a non-interactive file.

See also PYTHONUNBUFFERED.

https://docs.python.org/3.6/using/cmdline.html#envvar-PYTHONUNBUFFERED
PYTHONUNBUFFERED
If this is set to a non-empty string it is equivalent to specifying the -u option.



Python3.7

https://docs.python.org/3.7/using/cmdline.html#cmdoption-u
-u
Force the stdout and stderr streams to be unbuffered. This option has no effect on the stdin stream.

See also PYTHONUNBUFFERED.

Changed in version 3.7: The text layer of the stdout and stderr streams now is unbuffered.

https://docs.python.org/3.7/using/cmdline.html#envvar-PYTHONUNBUFFERED
PYTHONUNBUFFERED
If this is set to a non-empty string it is equivalent to specifying the -u option.
2019-01-15 15:52:08 +08:00
回复了 OhYee 创建的主题 问与答 为什么无缓冲的 stderr 会比有缓冲的 stdout 输出的还晚?
@kkk330 @swulling

OK, 明白了, python 里 stderr 是 line-buffer。
查到的资料有点混乱,而且 py2 的解释比较多所以搞乱了。
感谢两位大佬的解释。

另外关于-u 会截断输出内容的方面有什么解释么?
2019-01-15 15:19:19 +08:00
回复了 OhYee 创建的主题 问与答 为什么无缓冲的 stderr 会比有缓冲的 stdout 输出的还晚?
@kkk330

Interactive interpreter 不是指直接 python3 然后终端里写代码么
类似
$ python3
Python 3.6.7 (default, Oct 22 2018, 11:32:17)
[GCC 8.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>

https://docs.python.org/3/tutorial/interpreter.html

这种应该是属于 script 运行吧

另外,看了下文档,block-buffered 要怎么翻译啊, 块缓冲还是禁用缓冲?

我没有在官方文档找到百度上常见的 stderr 是无缓冲类似的解释
2019-01-15 15:04:45 +08:00
回复了 OhYee 创建的主题 问与答 为什么无缓冲的 stderr 会比有缓冲的 stdout 输出的还晚?
@kkk330

几种执行方式的结果来看

(因为被认为是外链,所以. 后面都加了空格)

python3 test. py 不符合预期,stderr 作为无 buffer 不应该在最后么?
python3 -u test. py 符合预期
python3 test. py >log. txt 符合预期
python3 test. py &>log. txt 符合预期



执行结果:
```
dev@u:~$ python3 test. py
stdout1 stdout2
0
1
2
3
4
5
6
7
8
9

stderr1 stderr2 dev@u:~$
dev@u:~$
dev@u:~$ python3 -u test. py
stdout1 stdout2
0
1
2
3
4
5
6
7
8
9

stderr1 stderr2 dev@u:~$
dev@u:~$
dev@u:~$ python3 test. py >log. txt
stderr1 stderr2 dev@u:~$
dev@u:~$ cat log. txt
stdout1 stdout2
0
1
2
3
4
5
6
7
8
9

dev@u:~$ python3 test. py &>log. txt
dev@u:~$ cat log. txt
stderr1 stderr2 stdout1 stdout2
0
1
2
3
4
5
6
7
8
9
```
2019-01-15 14:53:26 +08:00
回复了 OhYee 创建的主题 问与答 为什么无缓冲的 stderr 会比有缓冲的 stdout 输出的还晚?
@swulling
python3 里不也是 stdout 是 line buffer,stderr 是无 buffer 么

所以按照期望不管后面的内容有没有换行, stderr1 应该一定在 stdout2 前面吧

按照我测试的情况来看,stderr 也是 line-buffer,不过网上貌似都是说 stderr 是没有 buffer 的。

另外 13 楼的输出被截断也是 buffer 的问题么?
2019-01-15 14:51:05 +08:00
回复了 OhYee 创建的主题 问与答 为什么无缓冲的 stderr 会比有缓冲的 stdout 输出的还晚?
@kkk330
python test.py 运行的
不是直接终端运行的。

别的地方都和我理解的一样,只有混着 print 不符合理解
感觉 stderr 的无缓冲也不是简单的无缓冲

比如针对
'''python
string = ""
for i in range(10000):
string = string + str(i) + "\n"
'''

因为在不加-u 时,
sys.stderr.write(string)
sys.stdout.write(string)
可以正常输出(输出到 9999)

而加上-u,
sys.stderr.write(string)
sys.stdout.write(string)
两者都只能输出到 2638
2019-01-15 14:41:13 +08:00
回复了 OhYee 创建的主题 问与答 为什么无缓冲的 stderr 会比有缓冲的 stdout 输出的还晚?
@kkk330 stderr 不是无缓冲的么?他不应该直接输出不用等\n 么
2019-01-15 14:40:54 +08:00
回复了 OhYee 创建的主题 问与答 为什么无缓冲的 stderr 会比有缓冲的 stdout 输出的还晚?
stderr 不是无缓冲的么?他不应该直接输出不用等\n 么
2019-01-15 14:35:58 +08:00
回复了 OhYee 创建的主题 问与答 为什么无缓冲的 stderr 会比有缓冲的 stdout 输出的还晚?
把 print 换成 sys.stdout.write 符合预期了,所以说 print 不是一般意义的 stdout 么
2019-01-15 14:29:58 +08:00
回复了 OhYee 创建的主题 问与答 为什么无缓冲的 stderr 会比有缓冲的 stdout 输出的还晚?
@kkk330
行缓冲应该不会影响结果吧,就算是 print 的换行影响了结果,stderr 也应该在前面啊
2019-01-15 14:29:00 +08:00
回复了 OhYee 创建的主题 问与答 为什么无缓冲的 stderr 会比有缓冲的 stdout 输出的还晚?
@kkk330

我确认了下我是有开缓冲区的,没有加-u,PYTHONUNBUFFERED 也是空的

如果按照缓冲区的理解,输出结果应该是这样才对

```python
stderr1 stderr2 stdout1 stdout2
0
1
2
3
4
5
6
7
8
9

```

stderr 明明没有用缓冲区直接输出,竟然反而比走缓冲区的 stdout 还慢。
2019-01-07 13:42:40 +08:00
回复了 vtoexshan 创建的主题 程序员 安卓触屏上做手写笔记,有什么 apk 推荐吗?
OneNote
顺便,不如卖掉安卓换 Surface
2019-01-03 16:49:35 +08:00
回复了 julyedu 创建的主题 推广 程序员为什么 365 天背电脑包?
天天背包的路过,但是我还没毕业。
而且我真指不定大概或许不会用到
2018-12-28 19:40:38 +08:00
回复了 hero158 创建的主题 问与答 冬天,大家有什么取暖妙招?今天冻哭了。。。
抱着老婆相拥取暖
只好奇为什么不上传代码
2018-12-26 17:37:34 +08:00
回复了 willnill 创建的主题 程序员 请问点击阅读更多是哪个 nc 发明的?
这个功能很有意义,毕竟标题加摘要能让你决定要不要看这个文章。
但是不代表旁边放广告,点击要登录不反人类。
程序员要留着调试 ai,剩下的项目经理,产品经理,设计,人力资源更容易被替代掉吧。
留着投资人发钱就行。
1 ... 3  4  5  6  7  8  9  10  11  12 ... 15  
关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2885 人在线   最高记录 6543   ·     Select Language
创意工作者们的社区
World is powered by solitude
VERSION: 3.9.8.5 · 27ms · UTC 13:45 · PVG 21:45 · LAX 06:45 · JFK 09:45
Developed with CodeLauncher
♥ Do have faith in what you're doing.