In python có tuôn ra thiết bị xuất chuẩn không?

I do not know what are lib.logger.Log and lib.debugger.Tee, but I guess that lib.logger.Log[] creates a logger which writes to sys.stdout, and lib.debugger.Tee[] is a file-like object which writes to the specified file and to standard output.

Standard output of your program is not consumed, so your program writes to it until fill the buffer and then hangs. Add >/dev/null 2>/dev/null at the end of the command in cronjob to redirect stdout and stderr. This issue is not specific for Python, you can get the same issue with any program which outputs to stdout or stderr.

Tạo ngày 22-03-2011 10. 37 bởi techtonik, thay đổi lần cuối 2022-04-11 14. 57 bởi quản trị viên. vấn đề giờ đã kết thúc

- [view]Tác giả. anatoly techtonik [techtonik]Ngày. 2011-03-22 10. 37
print[] function for some reason buffers output on Windows if end!='\n'. 

In attached examples "Processing. " string is shown in Python 3.2 only after the actual processing is finished, while in Python 2.6 it is shown before.
- [view]Tác giả. STINNER Victor [vstinner] *
Ngày. 22-03-2011 11. 16
printtest2.py displays directly "Processing. " on Windows, but not on Linux. It looks like stdout is not buffered on Windows, which looks like a bug to bug :-] I think that it is safer to always call sys.stdout.flush[] to ensure that your message is directly displayed. With Python 2, you can use -u flag [unbuffered output] to avoid the explicit flush, but this is very inefficient [slow].

Python 3 uses line buffers, even with python3 -u, for better performances. If you want to see directly "Processing. ", as Python 2, call sys.stdout.flush[].

It is not a regression, it is a choice to be efficient.
- [view]Tác giả. anatoly techtonik [techtonik]Ngày. 22-03-2011 11. 33
From my perspective it is a regression on Windows and a bug in Linux version of Python 2.x, which unfortunately can not be fixed, because of 2.x release process.

If the fact that print statement doesn't output anything when called is not a bug - then should be documented long ago.
- [view]Tác giả. STINNER Victor [vstinner] *
Ngày. 22-03-2011 11. 47
> From my perspective it is a regression on Windows and a bug in Linux
> version of Python 2.x, which unfortunately can not be fixed,
> because of 2.x release process.

Line buffering is used by default on most operating systems [ok, maybe not Windows, which looks strange to me] and is not specific to Python.

Yes, Python has subtle differences on different operating systems, but at least it looks like Python 3 has the same behaviour on Linux and Windows ;-]

> If the fact that print statement doesn't output anything when called
> is not a bug - then should be documented long ago.

Can you please write a patch for the doc? Reopen the issue if you have a patch.
- [xem]Tác giả. anatoly techtonik [techtonik]Ngày. 22-03-2011 11. 49
How about making print[] user-friendly with flushing after every call, and if you want explicitly want speed - use buffered sys.stdout.write/flush[]?
- [xem]Tác giả. STINNER Victor [vstinner] *
Ngày. 22-03-2011 11. 57
> How about making print[] user-friendly with flushing after every call,
> and if you want explicitly want speed - use buffered
> sys.stdout.write/flush[]?

This is exactly the -u option of Python 2: use it if you would like a completly unbuffered sys.stdout in a portable way.

In Python 3, it is only useful to flush at each line, even if the output is not a console, but a pipe [e.g. output redirected to a file]. But nobody asked yet to have a fully unbuffered sys.stdout in Python 3.
- [xem]Tác giả. anatoly techtonik [techtonik]Ngày. 2011-03-22 12. 07
You must realize that the most common use case for print[..., end!='\n'] is when you want to notify user about intermediate progress of a very long operation.

Making documentation for simple print[] statement overloaded with low level buffering details makes language seem overly complicated for new users.
- [view]Tác giả. Amaury Forgeot d'Arc [amaury. forgeotdarc] *
Ngày. 22-03-2011 17. 28
We are talking about different things here:

- When python is run from a console, sys.stdout is line buffered. sys.stdout.write[] flushes if there is a carriage return. No need to change anything here.

- print[] could call file.flush[] if file.isatty[], *after* the multiple calls to file.write[].
- [view]Tác giả. Terry J. lau sậy [terry. sậy] *
Ngày. 2011-03-22 22. 44
Python 3.2, WinXP, IDLE edit window, F5 Run:
'Processing ...' appears immediately, 'Done' 3 sec later.
The only difference between printtest2/3 is where 'Done' appears.

Behavior is same when pasting into interactive interpreter -- has to be since the first two prints are executed before the sleep. I presume interpreter flushes before or after printing next prompt. IDLE must do same even when running from editor even when not printing prompts.

Anatoly, I gather you ran files by some other method.

I disagree with closing this yet. Print was designed to be a simplified wrapper around sys.stdout.write [and now, any file.write]. Requiring that one import sys to use print goes against that.

I have always experienced and expected Python's print to screen to be immediately visible. I thought that was pretty standard in other languages with a print-to-screen separate from general file-write. When printing to screen, efficiency is, I believe, a non-issue. Writing to files or the net is a whole different ballgame.

Amaury's idea of checking isatty seems good. Otherwise, print should gain an optional, no-default flush=True/False parameter [no default because behavior currently varies.] Until then, non-flushing *should* be documented. The current doc for print does not seem to address the issue either way.

Amaury, I though 'run from console' more or less meant 'isatty', so I am not sure I understand your comment.
- [xem]Tác giả. Amaury Forgeot d'Arc [amaury. forgeotdarc] *
Ngày. 2011-03-22 23. 27
print[] function for some reason buffers output on Windows if end!='\n'. 

In attached examples "Processing. " string is shown in Python 3.2 only after the actual processing is finished, while in Python 2.6 it is shown before.
0 - [xem]Tác giả. Amaury Forgeot d'Arc [amaury. forgeotdarc] *
Ngày. 2011-03-22 23. 29
printtest2.py displays directly "Processing. " on Windows, but not on Linux. It looks like stdout is not buffered on Windows, which looks like a bug to bug :-] I think that it is safer to always call sys.stdout.flush[] to ensure that your message is directly displayed. With Python 2, you can use -u flag [unbuffered output] to avoid the explicit flush, but this is very inefficient [slow].

Python 3 uses line buffers, even with python3 -u, for better performances. If you want to see directly "Processing. ", as Python 2, call sys.stdout.flush[].

It is not a regression, it is a choice to be efficient.
0 - [xem]Tác giả. STINNER Victor [vstinner] *
Ngày. 2011-03-23 ​​09. 25
printtest2.py displays directly "Processing. " on Windows, but not on Linux. It looks like stdout is not buffered on Windows, which looks like a bug to bug :-] I think that it is safer to always call sys.stdout.flush[] to ensure that your message is directly displayed. With Python 2, you can use -u flag [unbuffered output] to avoid the explicit flush, but this is very inefficient [slow].

Python 3 uses line buffers, even with python3 -u, for better performances. If you want to see directly "Processing. ", as Python 2, call sys.stdout.flush[].

It is not a regression, it is a choice to be efficient.
1 - [xem]Tác giả. Terry J. lau sậy [terry. sậy] *
Ngày. 23-03-2011 19. 25
printtest2.py displays directly "Processing. " on Windows, but not on Linux. It looks like stdout is not buffered on Windows, which looks like a bug to bug :-] I think that it is safer to always call sys.stdout.flush[] to ensure that your message is directly displayed. With Python 2, you can use -u flag [unbuffered output] to avoid the explicit flush, but this is very inefficient [slow].

Python 3 uses line buffers, even with python3 -u, for better performances. If you want to see directly "Processing. ", as Python 2, call sys.stdout.flush[].

It is not a regression, it is a choice to be efficient.
2 - [xem]Tác giả. anatoly techtonik [techtonik]Ngày. 2012-01-09 08. 02
printtest2.py displays directly "Processing. " on Windows, but not on Linux. It looks like stdout is not buffered on Windows, which looks like a bug to bug :-] I think that it is safer to always call sys.stdout.flush[] to ensure that your message is directly displayed. With Python 2, you can use -u flag [unbuffered output] to avoid the explicit flush, but this is very inefficient [slow].

Python 3 uses line buffers, even with python3 -u, for better performances. If you want to see directly "Processing. ", as Python 2, call sys.stdout.flush[].

It is not a regression, it is a choice to be efficient.
3 - [view]Tác giả. Terry J. lau sậy [terry. sậy] *
Ngày. 2012-01-09 11. 03
printtest2.py displays directly "Processing. " on Windows, but not on Linux. It looks like stdout is not buffered on Windows, which looks like a bug to bug :-] I think that it is safer to always call sys.stdout.flush[] to ensure that your message is directly displayed. With Python 2, you can use -u flag [unbuffered output] to avoid the explicit flush, but this is very inefficient [slow].

Python 3 uses line buffers, even with python3 -u, for better performances. If you want to see directly "Processing. ", as Python 2, call sys.stdout.flush[].

It is not a regression, it is a choice to be efficient.
4 - [view]Tác giả. anatoly techtonik [techtonik]Ngày. 2012-01-09 11. 35
printtest2.py displays directly "Processing. " on Windows, but not on Linux. It looks like stdout is not buffered on Windows, which looks like a bug to bug :-] I think that it is safer to always call sys.stdout.flush[] to ensure that your message is directly displayed. With Python 2, you can use -u flag [unbuffered output] to avoid the explicit flush, but this is very inefficient [slow].

Python 3 uses line buffers, even with python3 -u, for better performances. If you want to see directly "Processing. ", as Python 2, call sys.stdout.flush[].

It is not a regression, it is a choice to be efficient.
5 - [view]Tác giả. Éric Araujo [eric. araujo] *
Ngày. 2012-01-09 16. 59
printtest2.py displays directly "Processing. " on Windows, but not on Linux. It looks like stdout is not buffered on Windows, which looks like a bug to bug :-] I think that it is safer to always call sys.stdout.flush[] to ensure that your message is directly displayed. With Python 2, you can use -u flag [unbuffered output] to avoid the explicit flush, but this is very inefficient [slow].

Python 3 uses line buffers, even with python3 -u, for better performances. If you want to see directly "Processing. ", as Python 2, call sys.stdout.flush[].

It is not a regression, it is a choice to be efficient.
6 - [xem]Tác giả. anatoly techtonik [techtonik]Ngày. 2012-01-09 18. 41
printtest2.py displays directly "Processing. " on Windows, but not on Linux. It looks like stdout is not buffered on Windows, which looks like a bug to bug :-] I think that it is safer to always call sys.stdout.flush[] to ensure that your message is directly displayed. With Python 2, you can use -u flag [unbuffered output] to avoid the explicit flush, but this is very inefficient [slow].

Python 3 uses line buffers, even with python3 -u, for better performances. If you want to see directly "Processing. ", as Python 2, call sys.stdout.flush[].

It is not a regression, it is a choice to be efficient.
7 - ​​[xem]Tác giả. Terry J. lau sậy [terry. sậy] *
Ngày. 2012-01-09 19. 59
printtest2.py displays directly "Processing. " on Windows, but not on Linux. It looks like stdout is not buffered on Windows, which looks like a bug to bug :-] I think that it is safer to always call sys.stdout.flush[] to ensure that your message is directly displayed. With Python 2, you can use -u flag [unbuffered output] to avoid the explicit flush, but this is very inefficient [slow].

Python 3 uses line buffers, even with python3 -u, for better performances. If you want to see directly "Processing. ", as Python 2, call sys.stdout.flush[].

It is not a regression, it is a choice to be efficient.
8 - [xem]Tác giả. Robot Roundup [python-dev]
Ngày. 2012-01-11 19. 15
printtest2.py displays directly "Processing. " on Windows, but not on Linux. It looks like stdout is not buffered on Windows, which looks like a bug to bug :-] I think that it is safer to always call sys.stdout.flush[] to ensure that your message is directly displayed. With Python 2, you can use -u flag [unbuffered output] to avoid the explicit flush, but this is very inefficient [slow].

Python 3 uses line buffers, even with python3 -u, for better performances. If you want to see directly "Processing. ", as Python 2, call sys.stdout.flush[].

It is not a regression, it is a choice to be efficient.
9 - [xem]Tác giả. Terry J. lau sậy [terry. sậy] *
Ngày. 2012-01-11 19. 16
From my perspective it is a regression on Windows and a bug in Linux version of Python 2.x, which unfortunately can not be fixed, because of 2.x release process.

If the fact that print statement doesn't output anything when called is not a bug - then should be documented long ago.
0 - [view]Tác giả. Éric Araujo [eric. araujo] *
Ngày. 2012-01-12 17. 10
From my perspective it is a regression on Windows and a bug in Linux version of Python 2.x, which unfortunately can not be fixed, because of 2.x release process.

If the fact that print statement doesn't output anything when called is not a bug - then should be documented long ago.
1 - [xem]Tác giả. Robot Roundup [python-dev]
Ngày. 2012-01-12 19. 52
From my perspective it is a regression on Windows and a bug in Linux version of Python 2.x, which unfortunately can not be fixed, because of 2.x release process.

If the fact that print statement doesn't output anything when called is not a bug - then should be documented long ago.
2 - [xem]Tác giả. Éric Araujo [eric. araujo] *
Ngày. 2012-01-14 03. 18
From my perspective it is a regression on Windows and a bug in Linux version of Python 2.x, which unfortunately can not be fixed, because of 2.x release process.

If the fact that print statement doesn't output anything when called is not a bug - then should be documented long ago.
3 - [view]Tác giả. Cameron Simpson [cameron] *Ngày. 2012-01-14 03. 34
From my perspective it is a regression on Windows and a bug in Linux version of Python 2.x, which unfortunately can not be fixed, because of 2.x release process.

If the fact that print statement doesn't output anything when called is not a bug - then should be documented long ago.
4 - [view]Tác giả. Éric Araujo [eric. araujo] *
Ngày. 2012-01-14 04. 36
From my perspective it is a regression on Windows and a bug in Linux version of Python 2.x, which unfortunately can not be fixed, because of 2.x release process.

If the fact that print statement doesn't output anything when called is not a bug - then should be documented long ago.
5 - [view]Tác giả. Robot Roundup [python-dev]
Ngày. 2012-01-14 05. 07
From my perspective it is a regression on Windows and a bug in Linux version of Python 2.x, which unfortunately can not be fixed, because of 2.x release process.

If the fact that print statement doesn't output anything when called is not a bug - then should be documented long ago.
6

Bản in Python có tuôn ra không?

Phương thức in của Pythons dưới dạng thuộc tính độc quyền cụ thể là flush cho phép người dùng quyết định xem anh ta có muốn đầu ra của mình được lưu vào bộ đệm hay không. Giá trị mặc định của giá trị này là Sai có nghĩa là đầu ra sẽ được lưu vào bộ đệm. Nếu bạn thay đổi nó thành true, đầu ra sẽ được viết dưới dạng một chuỗi ký tự nối tiếp nhau.

Bản in bằng Python có chuyển sang thiết bị xuất chuẩn không?

print[] và Standard Out. Mọi chương trình đang chạy đều có một vùng đầu ra văn bản được gọi là "tiêu chuẩn" hoặc đôi khi chỉ là "thiết bị xuất chuẩn". Hàm print[] của Python nhận dữ liệu python chẳng hạn như số nguyên và chuỗi, rồi in các giá trị đó ra tiêu chuẩn .

Bản in có đi đến thiết bị xuất chuẩn không?

Thông thường, câu lệnh in được in ra thiết bị xuất chuẩn hoặc thiết bị đầu cuối/màn hình của bạn. Ví dụ, bạn có thể chuyển hướng điều này để việc in được thực hiện thành một tệp. Điều này có thể hữu ích nếu bạn sử dụng câu lệnh in để gỡ lỗi và sau đó muốn lưu nội dung được in vào một tệp.

In tuôn ra True Python là gì?

flush=False [có, mặc định] sẽ đợi dòng hoàn thành trước khi in. flush=True sẽ buộc thiết bị đầu cuối của chúng tôi in nó .

Chủ Đề