简体   繁体   中英

Python — suppress console output for a constantly running thread, not the entire program

BLUF: Is there a way to suppress console output just for a single thread?

I have a background thread that does system checks which include a bunch of pings to various things. These ping checks take awhile and then the results are flushed to the console. I don't want them to display at all, so I found that you can set stdout to devnull.

The problem there is that the system checks are in a while True loop (with a 60 second sleep between loops) and the checks themselves take up a good portion of that 60 seconds so I'm afraid suppressing console output during these functions will result in no console output for a good percentage of total runtime for the entire program -- console output I might later want to see from the main thread.

您可以将sys.stdout替换为基于线程过滤输出的自定义类。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM