반응형
리눅스 티는 파이썬과 함께 작동하지 않습니까?
저는 무한 루프를 사용하여 웹 서버와 통신하는 파이썬 스크립트를 만들었습니다.저는 모든 통신 데이터를 파일에 기록하고 단말기에서 동시에 모니터링하고 싶습니다.그래서 이렇게 티 커맨드를 사용했습니다.
python client.py | tee logfile
하지만 터미널이나 로그 파일에서 아무것도 받지 못했습니다.파이썬 스크립트는 잘 작동하고 있습니다.여기서 무슨 일이 일어나고 있나요? 제가 뭔가를 놓쳤나요?
조언을 해주시면 감사하겠습니다.잘 부탁드립니다.
부터man python
:
-u Force stdin, stdout and stderr to be totally unbuffered. On systems
where it matters, also put stdin, stdout and stderr in binary mode. Note
that there is internal buffering in xreadlines(), readlines() and file-
object iterators ("for line in sys.stdin") which is not influenced by
this option. To work around this, you will want to use "sys.stdin.read‐
line()" inside a "while 1:" loop.
따라서 다음과 같은 작업을 수행할 수 있습니다.
/usr/bin/python -u client.py >> logfile 2>&1
또는 사용tee
:
python -u client.py | tee logfile
완전히 버퍼링되지 않은 상태로 만드는 대신 일반적으로 라인 버퍼링됩니다.sys.stdout.reconfigure(line_buffering=True)
(이후import sys
물론).
이것은 3.7, 문서: https://docs.python.org/3/library/io.html#io.TextIOWrapper.reconfigure 에 추가되었습니다.
언급URL : https://stackoverflow.com/questions/21662783/linux-tee-is-not-working-with-python
반응형
'sourcetip' 카테고리의 다른 글
Oracle에서 프로세스가 두 번 이상 실행되지 않도록 하는 가장 좋은 방법이 있습니까? (0) | 2023.07.22 |
---|---|
파이썬에서 'r'은 문자열 앞에 무엇을 나타냅니까? (0) | 2023.07.22 |
C의 함수에서 재할당을 사용하는 방법 (0) | 2023.07.22 |
특정 장치에 있는 레코드에 따라 비디오 레코드의 모든 형제를 쿼리합니다. (0) | 2023.07.22 |
json 배열에서 mariadb의 JSON 함수를 사용하여 레코드를 검색하는 방법 (0) | 2023.07.22 |