Search code examples
pythonsocketsselectblockingfile-descriptor

Manipulate File Descriptors for select.select in Python


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.


Solution

  • 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.