Python - QueueHandler & QueueListener 範例

Python logging 模組提供很多內建的 handlers ,可以依照各種不一樣的使用情況選擇不同的 handler 處理日誌(log)。

然而,關於 QueueHandlers 的說明最為吸引我的目光:

Along with the QueueListener class, QueueHandler can be used to let handlers do their work on a separate thread from the one which does the logging.

This is important in Web applications and also other service applications where threads servicing clients need to respond as quickly as possible, while any potentially slow operations (such as sending an email via SMTPHandler) are done on a separate thread.

QueueHandler 與 QueueListener 讓我們能夠把 logging 的功能用另外的執行緒(thread)執行,如此一來像是 SMTPHandler 這種耗時間會拖慢 Web 應用或是其他服務的 handler 就可以在另外的執行緒執行,讓重要的服務能夠盡快地回應用戶,避免被 logging 拖慢回應速度。

Posted on  Nov 30, 2018  in  Python 程式設計 - 高階  by  Amo Chen  ‐ 2 min read