我在 python 中的 select 有问题,我有一段代码允许客户端从服务器接收数据,并通过在 stdin 上读取并在服务器套接字上写入来发送数据:
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
我认为这是因为选择是非阻塞的,但是当我让它阻塞时,这是同样的事情。你能解释一下我的代码中有什么错误吗?
Thanks
想必你总是可以写信给sys.stdout
so select.select
应该立即返回,让您知道您可以写信。然后,该代码处理可读列表,然后重新进入循环。但没有任何改变sys.stdout
所以它仍然是可写的。
这将在紧密循环中执行并烧毁 CPU。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)