简体   繁体   English

Python —取消控制台输出以获取持续运行的线程,而不是整个程序

[英]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? BLUF:有没有一种方法可以仅针对单个线程抑制控制台输出?

I have a background thread that does system checks which include a bunch of pings to various things. 我有一个后台线程,可以进行系统检查,其中包括对各种操作的一连串ping操作。 These ping checks take awhile and then the results are flushed to the console. 这些ping检查需要一段时间,然后将结果刷新到控制台。 I don't want them to display at all, so I found that you can set stdout to devnull. 我根本不希望它们显示,因此我发现可以将stdout设置为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. 问题是系统检查处于True循环(循环之间有60秒的睡眠),而检查本身占用了60秒的大部分时间,因此恐怕在这些功能中抑制控制台输出会导致在整个程序的总运行时中,没有控制台输出占了整个运行时间的很大一部分-稍后我可能想从主线程中看到控制台输出。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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