There is no doubt that print function is the most commonly used function in our daily life. Whether it is formatting output or printing intermediate variables for debugging, there is almost no work that print cannot do.

But last time, She almost got caught by Print.

Come from the pit

Originally, I wanted to add a progress display to one of my own command line gadgets, so I used the threading module to implement multiple threads, with one thread executing the actual logic and the other thread printing the current progress.

According to our

Many years of experience with the command line, the general print schedule is printed within the line, while Python’s print

By default, a newline character is printed at the end, which is not pretty.

Fortunately, print also provides an interface to change the end character of print. You can change the result of print by specifying the end parameter of print.

So I went to the trouble and changed the call to print(“#”, end=””) to print the progress.

Something like this:

But there was a big problem: the progress couldn’t be printed in real time.

That is, instead of the nice, cute # signs that should be printed out one by one during program execution, they are printed out to the console once the entire program is executed.

It grew up, and it grew ugly.

Then what do I need you for?

What’s the problem?

At the beginning of the sauce thought that the multithreading problems, silly everywhere to find information to “support” their various guesses – after the event is really too silly, so that now talking about or will hahaha

The lesson from this is: don’t be self-righteous, but to solve the problem earnestly and treat every detail humbly.

In fact, the reason we don’t see real time output is because we changed the ending character of print.

To minimize I/O operations, Python has a mechanism to cache output characters as much as possible, and only output the contents of the buffer to the corresponding stream once the end of a string, a newline, or a forced buffer flush is encountered.

The default newline character of print was removed, so that each print would trigger a buffer refresh. Instead, the buffer refresh would not be triggered until the program ended.

Flush () : sys.stdout.flush() : sys.stdout.flush() : sys.stdout.flush() : sys.stdout.flush() :

Aye? How nice is that?

These can be knowledge point, write down quickly write down, want to take an examination of

Let’s look at the official documentation for Print, which looks like this:

Whether the output of print in Python is buffered depends on two arguments: file and flush.

Some types of file need to be cached, such as sys.stdout. Others, such as sys.stderr, do not require buffering.

For the flush argument, whether the buffer depends on file when its value is False (the default); When it is True, the buffer is forced to flush.

Let’s modify the print call in the sample call:

It can also realize real-time printing of progress.

In addition, there is another way to implement real-time buffer flushing by adding a -u option when calling the program:

Of course, this method is not recommended, after all, the user of the program should not make any preset.

conclusion

This article is an account of a bizarre problem in Python that few people encounter.

All in all, to be a true Python programmer, it’s not enough to have basic syntax and a few clever tricks, but to have some understanding of Python itself.

After all, how can a swordsman walk the streets without knowing his sword?

This article reprinted text, copyright belongs to the author, such as infringement contact xiaobian delete!

The original address: www.tuicool.com/articles/iy…

Need source code or want to know moreClick here to download