MatUtter MatUtter - 1 year ago 92
Python Question

How to wait for any socket to have data?

I'm implementing a socket-client which opens several sockets at the same time. Any socket may have data at a different time and I want to execute code when any socket has data and is readable.

I'm not sure how to implement this, I was looking at but it seems to wait for all the sockets to be readable.

I'd like to avoid using multiprocessing to handle data on the sockets, I would like it to be serial in reading from each socket but read when there is data available.

How do I wait for any socket to be readable?

# psudo code

sockets = [sock1, sock2, sock3]

while True:
if len(sockets) == 0:
for sock in sockets:
if sock.has_data():

Answer Source

You can use for your problem:

sockets = [sock1, sock2, sock3]
while sockets:
    rlist, _, _ =, [], [])
    for sock in rlist:
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download