问题描述
我正在调整用于我自己的目的。脚本是一个简单的while循环守护程序,它根据,并将结果写入sys.stdout。 p>
我希望 sys.stdin.read()
阻止,直到它收到东西,但是我发现当我运行这个脚本,在任何数据发送或接收之前,它会占用100%的CPU。
-
sys.stdin
- 如果没有,我该如何让这个守护进程更有礼貌?
- 是
time.sleep(s)
安全使用,还是输入错误或响应迟缓?
默认情况下, sys.stdin.read()
和 sys.stdin。 read(n)
正在阻止调用。我会假设100%的CPU的消耗实际上归因于流式传输数据到您的脚本或其他一些其他未被引用的行为。
查看帮助文档 sys.stdin.read
,我注意到:
(重点是我的。)
这意味着阻止模式是默认行为,这符合我的经验。这也导致我跟踪关于SO的类似问题。 Voila:
祝你好运!
I'm adapting this Django management command for my own purposes. The script is a simple while-loop daemon that reads from sys.stdin (line 152, in command.handle()
) according to a protocol and writes results to sys.stdout.
I would expect sys.stdin.read()
to block until it receives something, but I find that when I run this script, it eats up 100% CPU before any data has been sent or received.
- Does
sys.stdin.read(n)
block? - If not, how can I make this daemon more polite?
- Is
time.sleep(s)
safe to use, or will I miss input or be slow to respond?
By default, sys.stdin.read()
and sys.stdin.read(n)
are blocking calls. I would assume the consumption of 100% CPU is actually attributable to streaming data into your script or some other behavior not cited here.
Upon looking at the help documentation for sys.stdin.read
, I noticed this:
(Emphasis mine.)
This implies blocking mode is the default behavior, which is consistent with my experience. It also led me to track down similar questions on SO. Voila:Non-blocking read on a subprocess.PIPE in python
Good luck with your adaptation!
这篇关于python的sys.stdin.read()块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!