【重磅推薦】Python – Supervisor如何記錄標準輸出 【一個很大的坑,解決普通print方法緩存問題】解決supervisor + gunicorn 無法實時屬性輸出的問題

參考https://www.icode9.com/content-1-335444.html

 

Python3默認的print方法會存在緩存,在生產環境運行尤其使用Supervisor運行flask不會輸出,除非每個print增加參數flush=True來刷新緩存【實時輸出】或直接用logging模塊方法

 

問題:配置supervisor + gunicorn  不實時刷新print日誌,使用python 直接運行 或直接 gunicorn 直接運行日誌沒問題

supervisor + gunicorn  只有當 重啓supervisor時才能看到輸出,不過啓動supervisor 後 運行一段時間,也會刷新日誌!

 

出現以上問題的根據就是Python的print方法存在緩存,要麼增加參數flush=Tru開啓刷新緩存,要麼使用logging.warning方法【更加推薦logging.warning方法,logging默認基本爲warning】

如果你要使用debug或info,則必須修改logging默認級別【建議輸出直接使用warning即可】

 

python 日誌logging級別及使用參考:https://www.cnblogs.com/qianxunman/p/12172994.html

python 日誌 logging模塊(詳細解析):https://blog.csdn.net/pansaky/article/details/90710751

 

 

(python 3)
# 【方式一】開始刷新緩存
print(something, flush=True)


# 【方式二,推薦使用】logging必須使用warning
import logging
logging.warning('Watch out!')

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章