Reputation: 3592
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.
Upvotes: 5
Views: 1955
Reputation: 2414
Using an unnamed pipe os.pipe() would also work well here (block on the read end, write to the write end) and keep you from having to specify a path.
I've found this sort of setup useful with multiple threads, when I want one thread to be able to interrupt another thread's blocking on a select call.
Upvotes: 0
Reputation: 799150
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.
Upvotes: 2
Reputation: 89517
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.
Upvotes: 0