以下是简化的代码。你知道吗
服务器.py
import sys
while True:
for line in sys.stdin:
# Do something here
客户端.py
import subprocess
import sys
p = subprocess.Popen([sys.executable, "server.py"], stdin=subprocess.PIPE)
当我跑服务器.py当stdin
中没有数据时,直接在for循环中读取的sys.stdin
处阻塞python。这是意料之中的。你知道吗
当我跑客户端.py,运行服务器.py在stdin连接到管道的子流程中,然后服务器.py在stdin中不再阻塞for循环读取,这导致我的CPU使用率达到100%。这是因为当stdin中没有可读取的内容时,循环将无限地运行,而不是阻塞。你知道吗
将stdin连接到子进程中的管道是什么导致了这种情况?有办法吗?我希望for循环在stdin没有可读取的数据时阻塞。你知道吗
目前没有回答
相关问题 更多 >
编程相关推荐