1

I use select like this:

while True:
    readReady, _, _ = select.select([src, dst], [], [])
    for ready in readReady:
        if ready == dst:
            toRead = dst
            toWrite = src
        else:
            toRead = src
            toWrite = dst

        toWrite.sendall(toRead.recv(10))

And it doesn't matter if toWrite blocks till all data is sent. But the problem is select immediately returns because the socket is ready to be read while it has no data. How do I wait till there is actually some data?

Also, print(toRead.recv(10)) prints b'' which is empty (while I expected select to block). Sockets are in blocking mode.

4

1 回答 1

2

这是selectAPI 的一部分:如果selectcall 返回一个可读的套接字,你必须调用recv()它,否则每次调用 select 都会返回这个套接字仍然可读。

如果您recv()并没有得到任何数据,那么这表明套接字已关闭。

于 2013-10-16T18:47:46.613 回答