sourcetip

리눅스 티는 파이썬과 함께 작동하지 않습니까?

fileupload 2023. 7. 22. 10:22
반응형

리눅스 티는 파이썬과 함께 작동하지 않습니까?

저는 무한 루프를 사용하여 웹 서버와 통신하는 파이썬 스크립트를 만들었습니다.저는 모든 통신 데이터를 파일에 기록하고 단말기에서 동시에 모니터링하고 싶습니다.그래서 이렇게 티 커맨드를 사용했습니다.

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

반응형