简体   繁体   English

如何判断是否已在Python中刷新sys.stdout

[英]How to tell whether sys.stdout has been flushed in Python

I'm trying to debug some code I wrote, which involves a lot of parallel processes. 我正在尝试调试我编写的一些代码,这涉及很多并行进程。 And have some unwanted behaviour involving output to sys.stdout and certain messages being printed twice. 并且有一些不需要的行为涉及到sys.stdout输出和某些消息被打印两次。 For debugging purposes it would be very useful to know whether at a certain point sys.stdout has been flushed or not. 出于调试目的,知道是否已刷新sys.stdout在某一点非常有用。 I wonder if this is possible and if so, how? 我想知道这是否可能,如果是的话,怎么样?

Ps. PS。 I don't know if it matters but I'm using OS X (at least some sys commands depend on the operating system). 我不知道它是否重要,但我使用的是OS X(至少有些sys命令取决于操作系统)。

The answer is: you cannot tell (not without serious uglyness, an external C module or similar). 答案是:你无法分辨(不是没有严重的丑陋,外部C模块或类似)。

The reason is that python's file-implementation is based on the C (stdio) implementation for FILE * . 原因是python的文件实现基于FILE *的C(stdio)实现。 So an underlying python file object basically just has a reference to the opened FILE . 所以底层的python文件对象基本上只有对打开的FILE的引用。 When writing data the C-implementation writes data, and when you tell it to flush() python also just forwards the flush call. 在写数据时,C实现写入数据,当你告诉它flush() python也只是转发flush调用。 So python does not hold the information. 所以python不保存信息。 Even for the underlying C layer, a quick search returned that there is no documented API allowing you to access this information, however it's probably somewhere in the FILE object, so it could in theory be read out if it is that desperately needed. 即使对于底层C层,快速搜索也会返回没有记录的API允许您访问此信息,但它可能位于FILE对象中的某个位置,因此理论上可以读出它是否是非常需要的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM