views:

40

answers:

3

Hello everyone,

I have an itching problem I know could be solved using many different ways, but I would still like to know if the following approach is possible in Python.

Suppose I have some socket I am constantly waiting for input on, and there is some condition that eventually terminates the whole program. I want to do it in a BLOCKING fashion, as I imagined, using select.select:

readfds, writefds, errfds = select.select([mysocket],[],[])
if readfds:
    conn, addr = mysocket.accept()
    ...

Now, if there is some file descriptor fd, that I can manually set to a ready condition, either read or write, I can do

readfds, writefds, errfds = select.select([mysocket,fd],[],[])
for r in readfds:
    if r == mysocket:
        conn, addr = mysocket.accept()
        ...
    else:
        <terminate>

Of course, I can simply send a message to mysocket, causing it to unblock but I would still like to know if there is a programmatic way to manipulate a file descriptor to a ready state.

EDIT: My question is: can I somehow set a file descriptor to "ready" manually?

Thanks all.

A: 

Yes, your code would work. Have you tried it? What problems did you find?

nosklo
Oh I guess, my question was how to set this "fd" to ready manually.. if I simply open a file it is always readable/writable.
e_x_p
@e_x_p: "I guess, my question was how to set this "fd" to ready manually"? If that's your question, please actually **update** the words you posted to reflect what your actual question is, please.
S.Lott
A: 

I think that you will have to create a socket pair (see the socket.socketpair() function) and spawn a separate Python thread (use the threading.Thread class) to watch for the special condition that tells your program when to end. When the thread detects the condition, it can write "Done!" (or whatever) on to its end of the socket pair. If you have the other end of the socket pair in the list of sockets you are waiting on for reading, then it will light up and say that it is readable as soon as the "Done!" appears and is ready to be read off of the socket.

Brandon Craig Rhodes
A: 

The easiest thing to do is probably to use os.mkfifo() to create a file pair, add the read end to the select() call, and then write to the write end when you want to unblock.

Also, you might want to consider just adding a timeout to the select() call; I can't imagine that you'd be doing enough during the unblocked time to drag performance down.

Ignacio Vazquez-Abrams
Thanks that's the type of thing I was looking for. Learned something new today!
e_x_p