我在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
Run Code Online (Sandbox Code Playgroud)
我认为这是因为选择是非阻塞的,但当我阻止它时,它是同样的事情.你能解释我的代码中的错误吗?
谢谢
大概你总是可以写信给sys.stdout所以select.select应该马上回来让你知道你可以写点什么.然后,此代码处理可读列表,然后重新进入循环.但是没有任何改变,sys.stdout所以它仍然是可写的.
这将在紧密循环中执行并刻录CPU.