首页 > 解决方案 > 用于 Windows Python 的 Git 打印在 DEBUG/LOG 时也不起作用?

问题描述

当我运行我的 Python 脚本时,我的打印语句不会被写入输出/屏幕(见下面的输出)。但是,在普通的 Windows 控制台/提示(和 macOS)中,它可以工作:日志和打印消息都写入标准输出(混合)。

DEBUG:session_storage:session:getter: MainThread
DEBUG:urllib3.connectionpool:Resetting dropped connection: example.com
DEBUG:urllib3.connectionpool:https://example.com:443 "GET /QW/7720d5b252de HTTP/1.1" 200 1073
before api init
!!setting session
b
after api init
after request init
Process  X-1  is running.
Got a valid value from queue

当我使用 CRTL-C 时,从“api init 之前”开始的文本会被打印出来(所以在程序执行之后)。

代码:

# SETUP LOGGING
logging.basicConfig(level=logging.DEBUG)

def main():

  #another process is created via multiprocessing (process duplicated?)
  ...

  logger = logging.getLogger(__name__)

if __name__ == '__main__':
  main()

也许它与多处理部分有关?

标签: pythonwindowsprintingoutput

解决方案


输出缓冲很可能是罪魁祸首。尝试刷新和消息应该出现

# for stdout
sys.stdout.flush()

# for stderr
sys.stderr.flush()

推荐阅读