【发布时间】:2013-06-30 04:20:27
【问题描述】:
我在 python 中的选择有问题,我有一段代码允许客户端从服务器接收数据并通过在标准输入上读取并在服务器套接字上写入来发送数据:
readfds = [s, sys.stdin]
writefds = [s, sys.stdout]
my_level = get_my_level(s)
is_co = True
cmd = ""
while (is_co):
read, write, exception = select.select(readfds, writefds, [], 1)
if (not (read or write or exception)):
print "Timeout"
else:
for sock in read:
if (sock == s):
cmd = readline(s)
print cmd
elif (sock == sys.stdin):
cmd = sys.stdin.readline()
s.sendall(cmd)
if (cmd == "mort"):
is_co = False
我认为这是因为选择是非阻塞的,但当我让它阻塞时,它是同一回事。你能解释一下我的代码中有错吗?
谢谢
【问题讨论】:
-
你能解释一下你的标题是什么意思吗?
-
您能详细解释一下问题所在吗?你的 CPU 达到 100%?如果你永远不会得到“mort”,那么你就有一个无限循环,所以你的程序会一遍又一遍地尝试......除了这似乎是设计使然。请提供更多信息。
-
当我使用这个选择启动我的代码时,我的 CPU 使用率达到 100%