You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: flush-print/README.md
+11-11Lines changed: 11 additions & 11 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -8,7 +8,7 @@ You don't need to install anything apart from a Python interpreter >=3.6 to run
8
8
9
9
## Countdown Script Variations
10
10
11
-
You can find all the code snippets that you've seen in the tutorial. For convenience, they're provided as separate files that all start with `countdown` and describe their differences with the rest of the file name. You can run each of the files separately without needing to edit the code, to see all the examples shown in the tutorial. Please visit the tutorial link for additional context.
11
+
You can find all the code snippets that you've seen in the tutorial. For convenience, they're provided as separate files that all start with `countdown` and describe their differences with the rest of the filename. To see all the examples shown in the tutorial, you can run each of the files separately without needing to edit the code. Please visit the tutorial link for additional context.
12
12
13
13
## Terminal Based Visual Progress Indicators
14
14
@@ -17,24 +17,24 @@ Additionally, this directory also contains two scripts that show example impleme
17
17
-[`spinner.py`](spinner.py)
18
18
-[`progress.py`](progress.py)
19
19
20
-
Both of them utilize explicit flushing and you can learn more about how to build them in [Your Guide to the Python `print()` Function](https://realpython.com/python-print/).
20
+
Both of them utilize explicit flushing, and you can learn more about how to build them in [Your Guide to the Python `print()` Function](https://realpython.com/python-print/).
21
21
22
22
## Buffer Size Approximations
23
23
24
24
Finally, if you're curious to dive somewhat deeper into the rabbit hole of buffering in Python, you can take a look at [`buffersize.py`](buffersize.py).
25
25
26
-
There doesn't seem to be a way to get the size of the default buffer for stdout on your operating system directly through a Python object. If you know or find a way, please [contact the author](#about-the-author), I'd be very curious to know :)
26
+
There doesn't seem to be a way to get the size of the default buffer for stdout on your operating system directly through a Python object. If you know or find a way, please [contact the author](#about-the-author). I'd be very curious to know :)
27
27
28
-
This script gives you a way to approximate the buffer size that Python uses when writing to stdout on your operating system and configuration. Note that this can be quite different between different setups.
28
+
This script gives you a way to approximate the buffer size that Python uses when writing to stdout on your operating system and configuration. Note that this can be quite different across different setups.
29
29
30
30
The approach to calculate it is somewhat manual:
31
31
32
-
1. Run the script
33
-
1. Note if the numbers that it prints pause in between before the script finishes execution
34
-
- If they pause, remember the last number that got printed before the pause
35
-
- If they don't pause, increase the value of `SLIGHTLY_TOO_LARGE_FOR_BUFFER` by `10_000` and start from the top
32
+
1. Run the script.
33
+
2. Note if the numbers that it prints pause in between before the script finishes execution.
34
+
- If they pause, remember the last number that got printed before the pause.
35
+
- If they don't pause, increase the value of `SLIGHTLY_TOO_LARGE_FOR_BUFFER` by `10_000` and start from the top.
36
36
37
-
Continue doing this until your script paused when printing the numbers, and you noted the number that displayed last during the pause. Once you have that number, manually subtract it from the value that `SLIGHTLY_TOO_LARGE_FOR_BUFFER` currently has to calculate your buffer size estimation, for example:
37
+
Continue doing this until your script pauses when printing the numbers, and note the number that displays last during the pause. Once you have that number, manually subtract it from the value that `SLIGHTLY_TOO_LARGE_FOR_BUFFER` currently has to calculate your buffer size estimation. For example:
38
38
39
39
```python
40
40
SLIGHTLY_TOO_LARGE_FOR_BUFFER=80_000
@@ -45,13 +45,13 @@ bufsize = 80_000 - 10919
45
45
print(bufsize) # 69081 <-- Your buffer size approximation
46
46
```
47
47
48
-
You can divide the number you get by `1000` to get an estimation of your buffer size for stdout in Kilobytes. In the example above, on a macOS system with a M1 chip, the buffer size of stdout when interacting with it through Python's `print()` would therefore be approximately 69 Kilobytes.
48
+
You can divide the number you get by `1000` to get an estimation of your buffer size for stdout in kilobytes. In the example above, on a macOS system with a M1 chip, the buffer size of stdout when interacting with it through Python's `print()` would therefore be approximately 69 kilobytes.
49
49
50
50
If you want to, you could even drill down further with some manual binary search to find the exact byte when the break first occurs.
51
51
52
52
The idea of this script is that each of these printed numbers take up exactly 6 bytes in the buffer. This is assuming that you're using an encoding in which numbers and spaces take up one byte. If so, then you can make sure each length is 6 characters with the format specifier in the f-string that you pass to `print()`.
53
53
54
-
At some point the buffer gets filled up so much that it needs to flush during the execution of your script. After it's flushed, it'll begin to fill up again. Ideally before it fills up again, you run into the call to `time.sleep()` executing. Finally, when the script finishes execution after the sleep, Python flushes the rest of the buffer contents, which prints the remaining numbers to your terminal.
54
+
At some point, the buffer gets filled up so much that it needs to flush during the execution of your script. After it's flushed, it'll begin to fill up again. Ideally before it fills up again, you run into the call to `time.sleep()` executing. Finally, when the script finishes execution after the sleep, Python flushes the rest of the buffer contents, which prints the remaining numbers to your terminal.
55
55
56
56
Finally, you need to subtract from the current value of `SLIGHTLY_TOO_LARGE_FOR_BUFFER` because we assume that the buffer flushes continuously when it gets full, which means that whatever is in the buffer when the script is done should represent a full buffer.
0 commit comments