Hướng dẫn how to print buffer in python - làm thế nào để in bộ đệm trong python

Bộ đệm đầu ra có được bật theo mặc định trong trình thông dịch của Python cho

# reopen stdout file descriptor with write mode
# and 0 as the buffer size [unbuffered]
import io, os, sys
try:
    # Python 3, open as binary, then wrap in a TextIOWrapper with write-through.
    sys.stdout = io.TextIOWrapper[open[sys.stdout.fileno[], 'wb', 0], write_through=True]
    # If flushing on newlines is sufficient, as of 3.7 you can instead just call:
    # sys.stdout.reconfigure[line_buffering=True]
except TypeError:
    # Python 2
    sys.stdout = os.fdopen[sys.stdout.fileno[], 'w', 0]
7 không?

Nếu câu trả lời là tích cực, tất cả các cách để vô hiệu hóa nó là gì?

Đề xuất cho đến nay:

  1. Sử dụng công tắc dòng lệnh
    # reopen stdout file descriptor with write mode
    # and 0 as the buffer size [unbuffered]
    import io, os, sys
    try:
        # Python 3, open as binary, then wrap in a TextIOWrapper with write-through.
        sys.stdout = io.TextIOWrapper[open[sys.stdout.fileno[], 'wb', 0], write_through=True]
        # If flushing on newlines is sufficient, as of 3.7 you can instead just call:
        # sys.stdout.reconfigure[line_buffering=True]
    except TypeError:
        # Python 2
        sys.stdout = os.fdopen[sys.stdout.fileno[], 'w', 0]
    
    8
  2. Bọc
    # reopen stdout file descriptor with write mode
    # and 0 as the buffer size [unbuffered]
    import io, os, sys
    try:
        # Python 3, open as binary, then wrap in a TextIOWrapper with write-through.
        sys.stdout = io.TextIOWrapper[open[sys.stdout.fileno[], 'wb', 0], write_through=True]
        # If flushing on newlines is sufficient, as of 3.7 you can instead just call:
        # sys.stdout.reconfigure[line_buffering=True]
    except TypeError:
        # Python 2
        sys.stdout = os.fdopen[sys.stdout.fileno[], 'w', 0]
    
    7 trong một đối tượng xả sau mỗi lần ghi
  3. Đặt
    import functools
    print = functools.partial[print, flush=True]
    
    0 env var
  4. import functools
    print = functools.partial[print, flush=True]
    
    1

Có cách nào khác để đặt một số cờ toàn cầu trong ____ 22/________ 17 lập trình trong quá trình thực hiện không?

WhyMarrh

12.7K14 Huy hiệu vàng57 Huy hiệu bạc106 Huy hiệu đồng14 gold badges57 silver badges106 bronze badges

Hỏi ngày 20 tháng 9 năm 2008 lúc 9:17Sep 20, 2008 at 9:17

Eli Benderskyeli BenderskyEli Bendersky

254K87 Huy hiệu vàng344 Huy hiệu bạc408 Huy hiệu đồng87 gold badges344 silver badges408 bronze badges

3

Từ Magnus Lyckka Trả lời trong danh sách gửi thư:

Bạn có thể bỏ qua bộ đệm cho toàn bộ quy trình Python bằng cách sử dụng

import functools
print = functools.partial[print, flush=True]
4 [hoặc
import functools
print = functools.partial[print, flush=True]
5, v.v.] hoặc bằng cách đặt biến môi trường pythonunbuffered.

Bạn cũng có thể thay thế sys.stdout bằng một số luồng khác như trình bao bọc thực hiện một cuộc gọi sau mỗi cuộc gọi.

class Unbuffered[object]:
   def __init__[self, stream]:
       self.stream = stream
   def write[self, data]:
       self.stream.write[data]
       self.stream.flush[]
   def writelines[self, datas]:
       self.stream.writelines[datas]
       self.stream.flush[]
   def __getattr__[self, attr]:
       return getattr[self.stream, attr]

import sys
sys.stdout = Unbuffered[sys.stdout]
print 'Hello'

pbies

63410 Huy hiệu bạc26 Huy hiệu Đồng10 silver badges26 bronze badges

Đã trả lời ngày 20 tháng 9 năm 2008 lúc 9:24Sep 20, 2008 at 9:24

15

# reopen stdout file descriptor with write mode
# and 0 as the buffer size [unbuffered]
import io, os, sys
try:
    # Python 3, open as binary, then wrap in a TextIOWrapper with write-through.
    sys.stdout = io.TextIOWrapper[open[sys.stdout.fileno[], 'wb', 0], write_through=True]
    # If flushing on newlines is sufficient, as of 3.7 you can instead just call:
    # sys.stdout.reconfigure[line_buffering=True]
except TypeError:
    # Python 2
    sys.stdout = os.fdopen[sys.stdout.fileno[], 'w', 0]

Tín dụng: "Sebastian", ở đâu đó trong danh sách gửi thư Python.

Đã trả lời ngày 8 tháng 10 năm 2008 lúc 7:23Oct 8, 2008 at 7:23

7

Vâng, đúng vậy.

Bạn có thể vô hiệu hóa nó trên dòng lệnh với công tắc "-U".

Ngoài ra, bạn có thể gọi .flush [] trên sys.stdout trên mỗi lần ghi [hoặc bọc nó bằng một đối tượng tự động thực hiện điều này]

Đã trả lời ngày 20 tháng 9 năm 2008 lúc 9:25Sep 20, 2008 at 9:25

BrianbrianBrian

Huy hiệu vàng 115K2828 gold badges107 silver badges111 bronze badges

0

Điều này liên quan đến câu trả lời của Cristóvão D. Sousa, nhưng tôi chưa thể bình luận.

Một cách thẳng thắn sử dụng đối số từ khóa

import functools
print = functools.partial[print, flush=True]
6 của Python 3 để luôn luôn có đầu ra không bị ảnh hưởng là:always have unbuffered output is:

import functools
print = functools.partial[print, flush=True]

Sau đó, in sẽ luôn tuôn ra đầu ra trực tiếp [trừ

import functools
print = functools.partial[print, flush=True]
7 được đưa ra].

Lưu ý, [a] rằng điều này trả lời câu hỏi chỉ một phần vì nó không chuyển hướng tất cả các đầu ra. Nhưng tôi đoán

import functools
print = functools.partial[print, flush=True]
8 là cách phổ biến nhất để tạo đầu ra thành ________ 29/________ 30 trong Python, vì vậy 2 dòng này có thể bao gồm hầu hết các trường hợp sử dụng.

Lưu ý [b] rằng nó chỉ hoạt động trong mô -đun/tập lệnh nơi bạn đã xác định nó. Điều này có thể tốt khi viết một mô -đun vì nó không gây rối với

# reopen stdout file descriptor with write mode
# and 0 as the buffer size [unbuffered]
import io, os, sys
try:
    # Python 3, open as binary, then wrap in a TextIOWrapper with write-through.
    sys.stdout = io.TextIOWrapper[open[sys.stdout.fileno[], 'wb', 0], write_through=True]
    # If flushing on newlines is sufficient, as of 3.7 you can instead just call:
    # sys.stdout.reconfigure[line_buffering=True]
except TypeError:
    # Python 2
    sys.stdout = os.fdopen[sys.stdout.fileno[], 'w', 0]
7.

Python 2 không cung cấp đối số

import functools
print = functools.partial[print, flush=True]
6, nhưng bạn có thể mô phỏng chức năng Python 3 loại
import functools
print = functools.partial[print, flush=True]
8 như được mô tả ở đây //stackoverflow.com/a/27991478/3734258.

Đã trả lời ngày 20 tháng 10 năm 2016 lúc 18:30Oct 20, 2016 at 18:30

Tim timTim

6531 Huy hiệu vàng6 Huy hiệu bạc13 Huy hiệu đồng1 gold badge6 silver badges13 bronze badges

2

def disable_stdout_buffering[]:
    # Appending to gc.garbage is a way to stop an object from being
    # destroyed.  If the old sys.stdout is ever collected, it will
    # close[] stdout, which is not good.
    gc.garbage.append[sys.stdout]
    sys.stdout = os.fdopen[sys.stdout.fileno[], 'w', 0]

# Then this will give output in the correct order:
disable_stdout_buffering[]
print "hello"
subprocess.call[["echo", "bye"]]

Không lưu sys.stdout cũ, vô hiệu hóa_stdout_buffering [] không phải là idempotent và nhiều cuộc gọi sẽ dẫn đến một lỗi như thế này:

Traceback [most recent call last]:
  File "test/buffering.py", line 17, in 
    print "hello"
IOError: [Errno 9] Bad file descriptor
close failed: [Errno 9] Bad file descriptor

Một khả năng khác là:

def disable_stdout_buffering[]:
    fileno = sys.stdout.fileno[]
    temp_fd = os.dup[fileno]
    sys.stdout.close[]
    os.dup2[temp_fd, fileno]
    os.close[temp_fd]
    sys.stdout = os.fdopen[fileno, "w", 0]

.

Đã trả lời ngày 9 tháng 9 năm 2010 lúc 15:37Sep 9, 2010 at 15:37

Mark Seabornmark SeabornMark Seaborn

1.33213 huy hiệu bạc11 huy hiệu đồng13 silver badges11 bronze badges

3

Các tác phẩm sau đây trong Python 2.6, 2.7 và 3.2:

import os
import sys
buf_arg = 0
if sys.version_info[0] == 3:
    os.environ['PYTHONUNBUFFERED'] = '1'
    buf_arg = 1
sys.stdout = os.fdopen[sys.stdout.fileno[], 'a+', buf_arg]
sys.stderr = os.fdopen[sys.stderr.fileno[], 'a+', buf_arg]

Đã trả lời ngày 12 tháng 4 năm 2014 lúc 18:43Apr 12, 2014 at 18:43

GummbumgummumGummbum

Huy hiệu Huy hiệu Bạc 14111 silver badge2 bronze badges

3

Có, nó được bật theo mặc định. Bạn có thể vô hiệu hóa nó bằng cách sử dụng tùy chọn -U trên dòng lệnh khi gọi Python.

Đã trả lời ngày 20 tháng 9 năm 2008 lúc 9:26Sep 20, 2008 at 9:26

NathannathanNathan

11.6K12 Huy hiệu vàng54 Huy hiệu bạc62 Huy hiệu đồng12 gold badges54 silver badges62 bronze badges

Trong Python 3, bạn có thể hút hàm chức năng in, để luôn gửi flush = true:

_orig_print = print

def print[*args, **kwargs]:
    _orig_print[*args, flush=True, **kwargs]

Như đã chỉ ra trong một nhận xét, bạn có thể đơn giản hóa điều này bằng cách ràng buộc tham số Flush với một giá trị, thông qua

def disable_stdout_buffering[]:
    # Appending to gc.garbage is a way to stop an object from being
    # destroyed.  If the old sys.stdout is ever collected, it will
    # close[] stdout, which is not good.
    gc.garbage.append[sys.stdout]
    sys.stdout = os.fdopen[sys.stdout.fileno[], 'w', 0]

# Then this will give output in the correct order:
disable_stdout_buffering[]
print "hello"
subprocess.call[["echo", "bye"]]
4:

print = functools.partial[print, flush=True]

Đã trả lời ngày 26 tháng 11 năm 2018 lúc 20:07Nov 26, 2018 at 20:07

OliveroliverOliver

25.7K8 Huy hiệu vàng66 Huy hiệu bạc93 Huy hiệu Đồng8 gold badges66 silver badges93 bronze badges

5

Bạn cũng có thể chạy Python với tiện ích Stdbuf:

def disable_stdout_buffering[]:
    # Appending to gc.garbage is a way to stop an object from being
    # destroyed.  If the old sys.stdout is ever collected, it will
    # close[] stdout, which is not good.
    gc.garbage.append[sys.stdout]
    sys.stdout = os.fdopen[sys.stdout.fileno[], 'w', 0]

# Then this will give output in the correct order:
disable_stdout_buffering[]
print "hello"
subprocess.call[["echo", "bye"]]
5

Đã trả lời ngày 1 tháng 7 năm 2015 lúc 20:09Jul 1, 2015 at 20:09

Dyomasdyomasdyomas

6905 Huy hiệu bạc13 Huy hiệu Đồng5 silver badges13 bronze badges

3

Bạn có thể tạo một tệp không bị ảnh hưởng và gán tệp này cho sys.stdout.

import sys 
myFile= open[ "a.log", "w", 0 ] 
sys.stdout= myFile

Bạn không thể thay đổi một cách kỳ diệu các stdout do hệ thống cung cấp; Vì nó được cung cấp cho chương trình Python của bạn bởi HĐH.

Đã trả lời ngày 20 tháng 9 năm 2008 lúc 10:39Sep 20, 2008 at 10:39

S.LottS.LottS.Lott

378K79 Huy hiệu vàng503 Huy hiệu bạc773 Huy hiệu Đồng79 gold badges503 silver badges773 bronze badges

1

Bạn cũng có thể sử dụng FCNTL để thay đổi các cờ tệp trong Fly.

# reopen stdout file descriptor with write mode
# and 0 as the buffer size [unbuffered]
import io, os, sys
try:
    # Python 3, open as binary, then wrap in a TextIOWrapper with write-through.
    sys.stdout = io.TextIOWrapper[open[sys.stdout.fileno[], 'wb', 0], write_through=True]
    # If flushing on newlines is sufficient, as of 3.7 you can instead just call:
    # sys.stdout.reconfigure[line_buffering=True]
except TypeError:
    # Python 2
    sys.stdout = os.fdopen[sys.stdout.fileno[], 'w', 0]
0

Đã trả lời ngày 15 tháng 11 năm 2009 lúc 0:01Nov 15, 2009 at 0:01

Jimxjimxjimx

1.0022 huy hiệu vàng12 Huy hiệu bạc12 Huy hiệu đồng2 gold badges12 silver badges12 bronze badges

2

Có thể chỉ ghi đè lên phương thức

def disable_stdout_buffering[]:
    # Appending to gc.garbage is a way to stop an object from being
    # destroyed.  If the old sys.stdout is ever collected, it will
    # close[] stdout, which is not good.
    gc.garbage.append[sys.stdout]
    sys.stdout = os.fdopen[sys.stdout.fileno[], 'w', 0]

# Then this will give output in the correct order:
disable_stdout_buffering[]
print "hello"
subprocess.call[["echo", "bye"]]
6 của
# reopen stdout file descriptor with write mode
# and 0 as the buffer size [unbuffered]
import io, os, sys
try:
    # Python 3, open as binary, then wrap in a TextIOWrapper with write-through.
    sys.stdout = io.TextIOWrapper[open[sys.stdout.fileno[], 'wb', 0], write_through=True]
    # If flushing on newlines is sufficient, as of 3.7 you can instead just call:
    # sys.stdout.reconfigure[line_buffering=True]
except TypeError:
    # Python 2
    sys.stdout = os.fdopen[sys.stdout.fileno[], 'w', 0]
7 với một phương thức gọi
import functools
print = functools.partial[print, flush=True]
6. Thực hiện phương pháp được đề xuất là dưới đây.

# reopen stdout file descriptor with write mode
# and 0 as the buffer size [unbuffered]
import io, os, sys
try:
    # Python 3, open as binary, then wrap in a TextIOWrapper with write-through.
    sys.stdout = io.TextIOWrapper[open[sys.stdout.fileno[], 'wb', 0], write_through=True]
    # If flushing on newlines is sufficient, as of 3.7 you can instead just call:
    # sys.stdout.reconfigure[line_buffering=True]
except TypeError:
    # Python 2
    sys.stdout = os.fdopen[sys.stdout.fileno[], 'w', 0]
1

Giá trị mặc định của đối số

def disable_stdout_buffering[]:
    # Appending to gc.garbage is a way to stop an object from being
    # destroyed.  If the old sys.stdout is ever collected, it will
    # close[] stdout, which is not good.
    gc.garbage.append[sys.stdout]
    sys.stdout = os.fdopen[sys.stdout.fileno[], 'w', 0]

# Then this will give output in the correct order:
disable_stdout_buffering[]
print "hello"
subprocess.call[["echo", "bye"]]
9 sẽ giữ cho tham chiếu phương thức
def disable_stdout_buffering[]:
    # Appending to gc.garbage is a way to stop an object from being
    # destroyed.  If the old sys.stdout is ever collected, it will
    # close[] stdout, which is not good.
    gc.garbage.append[sys.stdout]
    sys.stdout = os.fdopen[sys.stdout.fileno[], 'w', 0]

# Then this will give output in the correct order:
disable_stdout_buffering[]
print "hello"
subprocess.call[["echo", "bye"]]
6 gốc. Sau khi
Traceback [most recent call last]:
  File "test/buffering.py", line 17, in 
    print "hello"
IOError: [Errno 9] Bad file descriptor
close failed: [Errno 9] Bad file descriptor
1 được xác định,
def disable_stdout_buffering[]:
    # Appending to gc.garbage is a way to stop an object from being
    # destroyed.  If the old sys.stdout is ever collected, it will
    # close[] stdout, which is not good.
    gc.garbage.append[sys.stdout]
    sys.stdout = os.fdopen[sys.stdout.fileno[], 'w', 0]

# Then this will give output in the correct order:
disable_stdout_buffering[]
print "hello"
subprocess.call[["echo", "bye"]]
6 ban đầu có thể bị ghi đè.

# reopen stdout file descriptor with write mode
# and 0 as the buffer size [unbuffered]
import io, os, sys
try:
    # Python 3, open as binary, then wrap in a TextIOWrapper with write-through.
    sys.stdout = io.TextIOWrapper[open[sys.stdout.fileno[], 'wb', 0], write_through=True]
    # If flushing on newlines is sufficient, as of 3.7 you can instead just call:
    # sys.stdout.reconfigure[line_buffering=True]
except TypeError:
    # Python 2
    sys.stdout = os.fdopen[sys.stdout.fileno[], 'w', 0]
2

Mã giả định rằng

import functools
print = functools.partial[print, flush=True]
9 được nhập theo cách này
Traceback [most recent call last]:
  File "test/buffering.py", line 17, in 
    print "hello"
IOError: [Errno 9] Bad file descriptor
close failed: [Errno 9] Bad file descriptor
4.

Đã trả lời ngày 25 tháng 2 năm 2017 lúc 21:00Feb 25, 2017 at 21:00

Một cách để có được đầu ra không có nội dung sẽ là sử dụng

Traceback [most recent call last]:
  File "test/buffering.py", line 17, in 
    print "hello"
IOError: [Errno 9] Bad file descriptor
close failed: [Errno 9] Bad file descriptor
5 thay vì
# reopen stdout file descriptor with write mode
# and 0 as the buffer size [unbuffered]
import io, os, sys
try:
    # Python 3, open as binary, then wrap in a TextIOWrapper with write-through.
    sys.stdout = io.TextIOWrapper[open[sys.stdout.fileno[], 'wb', 0], write_through=True]
    # If flushing on newlines is sufficient, as of 3.7 you can instead just call:
    # sys.stdout.reconfigure[line_buffering=True]
except TypeError:
    # Python 2
    sys.stdout = os.fdopen[sys.stdout.fileno[], 'w', 0]
7 hoặc đơn giản là gọi
Traceback [most recent call last]:
  File "test/buffering.py", line 17, in 
    print "hello"
IOError: [Errno 9] Bad file descriptor
close failed: [Errno 9] Bad file descriptor
7 để buộc một cách viết một cách rõ ràng xảy ra.

Bạn có thể dễ dàng chuyển hướng mọi thứ được in bằng cách làm:

# reopen stdout file descriptor with write mode
# and 0 as the buffer size [unbuffered]
import io, os, sys
try:
    # Python 3, open as binary, then wrap in a TextIOWrapper with write-through.
    sys.stdout = io.TextIOWrapper[open[sys.stdout.fileno[], 'wb', 0], write_through=True]
    # If flushing on newlines is sufficient, as of 3.7 you can instead just call:
    # sys.stdout.reconfigure[line_buffering=True]
except TypeError:
    # Python 2
    sys.stdout = os.fdopen[sys.stdout.fileno[], 'w', 0]
3

Hoặc chuyển hướng chỉ cho một tuyên bố

import functools
print = functools.partial[print, flush=True]
8 cụ thể:

# reopen stdout file descriptor with write mode
# and 0 as the buffer size [unbuffered]
import io, os, sys
try:
    # Python 3, open as binary, then wrap in a TextIOWrapper with write-through.
    sys.stdout = io.TextIOWrapper[open[sys.stdout.fileno[], 'wb', 0], write_through=True]
    # If flushing on newlines is sufficient, as of 3.7 you can instead just call:
    # sys.stdout.reconfigure[line_buffering=True]
except TypeError:
    # Python 2
    sys.stdout = os.fdopen[sys.stdout.fileno[], 'w', 0]
4

Để đặt lại stdout, bạn có thể làm:

# reopen stdout file descriptor with write mode
# and 0 as the buffer size [unbuffered]
import io, os, sys
try:
    # Python 3, open as binary, then wrap in a TextIOWrapper with write-through.
    sys.stdout = io.TextIOWrapper[open[sys.stdout.fileno[], 'wb', 0], write_through=True]
    # If flushing on newlines is sufficient, as of 3.7 you can instead just call:
    # sys.stdout.reconfigure[line_buffering=True]
except TypeError:
    # Python 2
    sys.stdout = os.fdopen[sys.stdout.fileno[], 'w', 0]
5

efotinis

14.1k5 Huy hiệu vàng31 Huy hiệu bạc36 Huy hiệu đồng5 gold badges31 silver badges36 bronze badges

Đã trả lời ngày 20 tháng 9 năm 2008 lúc 9:40Sep 20, 2008 at 9:40

Stderrstderrstderr

8.4291 Huy hiệu vàng34 Huy hiệu bạc 50 Huy hiệu Đồng1 gold badge34 silver badges50 bronze badges

2

Biến thể hoạt động mà không gặp sự cố [ít nhất là trên Win32; Python 2.7, Ipython 0.12] sau đó được gọi sau đó [nhiều lần]:

# reopen stdout file descriptor with write mode
# and 0 as the buffer size [unbuffered]
import io, os, sys
try:
    # Python 3, open as binary, then wrap in a TextIOWrapper with write-through.
    sys.stdout = io.TextIOWrapper[open[sys.stdout.fileno[], 'wb', 0], write_through=True]
    # If flushing on newlines is sufficient, as of 3.7 you can instead just call:
    # sys.stdout.reconfigure[line_buffering=True]
except TypeError:
    # Python 2
    sys.stdout = os.fdopen[sys.stdout.fileno[], 'w', 0]
6

Đã trả lời ngày 30 tháng 6 năm 2012 lúc 19:20Jun 30, 2012 at 19:20

3

[Tôi đã đăng một bình luận, nhưng nó đã bị mất bằng cách nào đó. Vì vậy, một lần nữa :]

  1. Như tôi đã nhận thấy, CPython [ít nhất là trên Linux] hoạt động khác nhau tùy thuộc vào nơi đầu ra đi. Nếu nó đi đến một tty, thì đầu ra sẽ được xóa sau mỗi '

    Traceback [most recent call last]:
      File "test/buffering.py", line 17, in 
        print "hello"
    IOError: [Errno 9] Bad file descriptor
    close failed: [Errno 9] Bad file descriptor
    
    9 nếu nó đi vào một đường ống/quy trình, thì nó được đệm và bạn có thể sử dụng các giải pháp dựa trên
    def disable_stdout_buffering[]:
        fileno = sys.stdout.fileno[]
        temp_fd = os.dup[fileno]
        sys.stdout.close[]
        os.dup2[temp_fd, fileno]
        os.close[temp_fd]
        sys.stdout = os.fdopen[fileno, "w", 0]
    
    0 hoặc tùy chọn -U được đề xuất ở trên.
    If it goes to a pipe/process, then it is buffered and you can use the
    def disable_stdout_buffering[]:
        fileno = sys.stdout.fileno[]
        temp_fd = os.dup[fileno]
        sys.stdout.close[]
        os.dup2[temp_fd, fileno]
        os.close[temp_fd]
        sys.stdout = os.fdopen[fileno, "w", 0]
    
    0 based solutions or the -u option recommended above.

  2. Hơi liên quan đến bộ đệm đầu ra: Nếu bạn lặp lại các dòng trong đầu vào với
    If you iterate over the lines in the input with

    def disable_stdout_buffering[]:
        fileno = sys.stdout.fileno[]
        temp_fd = os.dup[fileno]
        sys.stdout.close[]
        os.dup2[temp_fd, fileno]
        os.close[temp_fd]
        sys.stdout = os.fdopen[fileno, "w", 0]
    
    1 ...
    ...

Sau đó, việc triển khai trong CPython sẽ thu thập đầu vào trong một thời gian và sau đó thực hiện thân vòng lặp cho một loạt các dòng đầu vào. Nếu tập lệnh của bạn sắp viết đầu ra cho từng dòng đầu vào, điều này có thể trông giống như bộ đệm đầu ra nhưng nó thực sự là hàng loạt, và do đó, không có kỹ thuật nào trong số

def disable_stdout_buffering[]:
    fileno = sys.stdout.fileno[]
    temp_fd = os.dup[fileno]
    sys.stdout.close[]
    os.dup2[temp_fd, fileno]
    os.close[temp_fd]
    sys.stdout = os.fdopen[fileno, "w", 0]
0, v.v. Thật thú vị, bạn không có hành vi này trong pypy. Để tránh điều này, bạn có thể sử dụngfor implementation in CPython will collect the input for a while and then execute the loop body for a bunch of input lines. If your script is about to write output for each input line, this might look like output buffering but it's actually batching, and therefore, none of the
def disable_stdout_buffering[]:
    fileno = sys.stdout.fileno[]
    temp_fd = os.dup[fileno]
    sys.stdout.close[]
    os.dup2[temp_fd, fileno]
    os.close[temp_fd]
    sys.stdout = os.fdopen[fileno, "w", 0]
0, etc. techniques will help that. Interestingly, you don't have this behaviour in pypy. To avoid this, you can use

def disable_stdout_buffering[]:
    fileno = sys.stdout.fileno[]
    temp_fd = os.dup[fileno]
    sys.stdout.close[]
    os.dup2[temp_fd, fileno]
    os.close[temp_fd]
    sys.stdout = os.fdopen[fileno, "w", 0]
3 ...
...

Đã trả lời ngày 11 tháng 6 năm 2013 lúc 14:47Jun 11, 2013 at 14:47

Tzptzptzp

5367 Huy hiệu bạc10 Huy hiệu Đồng7 silver badges10 bronze badges

3

Python in có được đệm không?

Theo mặc định, in trong Python được đệm..

Một bộ đệm trong Python là gì?

Các cấu trúc bộ đệm [hoặc đơn giản là bộ đệm trên mạng] rất hữu ích như một cách để hiển thị dữ liệu nhị phân từ một đối tượng khác đến lập trình viên Python.Chúng cũng có thể được sử dụng như một cơ chế cắt không sao.Sử dụng khả năng tham chiếu một khối bộ nhớ của họ, có thể hiển thị bất kỳ dữ liệu nào cho lập trình viên Python khá dễ dàng.a way to expose the binary data from another object to the Python programmer. They can also be used as a zero-copy slicing mechanism. Using their ability to reference a block of memory, it is possible to expose any data to the Python programmer quite easily.

Làm thế nào để bạn in một tuôn ra trong Python?

Làm thế nào để xả đầu ra của in python ?..
Trong Python 3, cuộc gọi in [..., flush = true] [đối số tuôn ra không có sẵn trong hàm in của Python 2 và không có tương tự cho câu lệnh in] ..
Gọi File.flush [] trên tệp đầu ra [chúng ta có thể kết thúc chức năng in của Python 2 để thực hiện việc này], ví dụ: sys.stdout ..

In là gì [] trong Python?

Hàm in [] in thông báo được chỉ định lên màn hình hoặc thiết bị đầu ra tiêu chuẩn khác.Thông báo có thể là một chuỗi hoặc bất kỳ đối tượng nào khác, đối tượng sẽ được chuyển đổi thành một chuỗi trước khi được ghi vào màn hình.prints the specified message to the screen, or other standard output device. The message can be a string, or any other object, the object will be converted into a string before written to the screen.

Bài Viết Liên Quan

Chủ Đề