1

I have a thread running that is looking for data from a socket connection. When it is empty it just sits there as I found it is supposed to, but I would like to pass by this when it is empty. I found that the select module should be able to do it, but now it just sits at the select line instead. I have tried this below from some research, but like I said, now instead of stopping at the data = sock.recv, it stops at the first line. Any ideas? Thanks.

while True:
        readable, writable, exceptional = select.select([sock], [], [])
        if readable:
            data = sock.recv(1024)
        else:
            do something

1 Answer 1

1

As you could have read in the documentation, select.select() takes an optional 4th argument, taking a timeout value.

If the timeout passes without sock becoming readable, readable is empty.

You can try something like

while True:
    readable, writable, exceptional = select.select([sock], [], [], 1.0)
    if readable:
        data = sock.recv(1024)
    else:
        print "nothing yet"
Sign up to request clarification or add additional context in comments.

5 Comments

readable, writable, exceptional = select.select([sock], [], [], timeout=1.0) TypeError: select() takes no keyword arguments - I did try that but get this error
That means you can't use a keyword argument, just pass the timeout instead: readable, writable, exceptional = select.select([sock], [], [], 1.0).
@user3582887 Ok, then I misunderstood that part of the documentation, but you already had the idea to try select.select([sock], [], [], 1.0) afterwards, didn't you?
@SiHa As "the optional timeout argument specifies a time-out as a floating point number in seconds", as the doc states, it is unlikely it would do so.
Thanks all, sorry, didn't have that idea, didn't understand the error, but appreciate all the input.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.