티스토리 뷰

반응형

Stack Overflow에 자주 검색, 등록되는 문제들과 제가 개발 중 찾아 본 문제들 중에서 나중에도 찾아 볼 것 같은 문제들을 정리하고 있습니다.

Stack Overflow에서 가장 먼저 확인하게 되는 가장 높은 점수를 받은 Solution과 현 시점에 도움이 될 수 있는 가장 최근에 업데이트(최소 점수 확보)된 Solution을 각각 정리하였습니다.

 

아래 word cloud를 통해 이번 포스팅의 주요 키워드를 미리 확인하세요.

How often does python flush to a file?

파이썬에서 파일에 대한 flush는 얼마나 자주 일어날까요?

 문제 내용 

  1. How often does Python flush to a file?
  2. How often does Python flush to stdout?
1. Python은 파일에 대해 얼마나 자주 flush하는가요?
2. Python은 stdout에 대해 얼마나 자주 flush하는가요?

 

I'm unsure about (1).

(1)에 대해서는 확실하지 않습니다.

 

As for (2), I believe Python flushes to stdout after every new line. But, if you overload stdout to be to a file, does it flush as often?

(2)에 대해서는 파이썬이 stdout에 대한 flush를 새로운 줄마다 하도록 설정되어 있다고 생각합니다. 그러나, stdout을 파일로 오버로드하는 경우에도 동일하게 자주 flush되는 것일까요?

 

 

 

 높은 점수를 받은 Solution 

For file operations, Python uses the operating system's default buffering unless you configure it do otherwise. You can specify a buffer size, unbuffered, or line buffered.

파일 작업의 경우, 파이썬은 운영 체제의 기본 버퍼링을 사용하며, 다른 방식으로 구성하지 않는 한 기본적으로 버퍼링이 적용됩니다. 버퍼 크기, 버퍼링되지 않음 또는 라인 버퍼링을 지정할 수 있습니다.

 

For example, the open function takes a buffer size argument.

예를 들어, open 함수는 버퍼 크기 인자를 취합니다.

 

http://docs.python.org/library/functions.html#open

 

"The optional buffering argument specifies the file’s desired buffer size:"

"옵션 버퍼링 인수는 파일의 원하는 버퍼 크기를 지정합니다."

 

  • 0 means unbuffered,
  • 1 means line buffered,
  • any other positive value means use a buffer of (approximately) that size.
  • A negative buffering means to use the system default, which is usually line buffered for tty devices and fully buffered for other files.
  • If omitted, the system default is used.
▪ 0은 버퍼링되지 않음,
▪ 1은 라인 버퍼링,
▪ 그 외의 양수 값은 대략 해당 크기의 버퍼를 사용합니다.
▪ 음수 버퍼링은 시스템 기본값을 사용하며, tty 장치에 대해서는 일반적으로 라인 버퍼링을 사용하고 다른 파일에 대해서는 완전 버퍼링을 사용합니다.
▪ 생략하면 시스템 기본값을 사용합니다.

 

code:

코드:
bufsize = 0
f = open('file.txt', 'w', buffering=bufsize)

 

 

 가장 최근 달린 Solution 

You can also check the default buffer size by calling the read only DEFAULT_BUFFER_SIZE attribute from io module.

또한 io 모듈에서 읽기 전용 DEFAULT_BUFFER_SIZE 속성을 호출하여 기본 버퍼 크기를 확인할 수도 있습니다.
import io
print (io.DEFAULT_BUFFER_SIZE)

 

 

출처 : https://stackoverflow.com/questions/3167494/how-often-does-python-flush-to-a-file

반응형
댓글
공지사항
최근에 올라온 글