select.select question

Donn Cave donn at drizzle.com
Fri Aug 23 02:37:09 EDT 2002


Quoth Heiko Wundram <hewu5001 at stud.uni-saarland.de>:

| I'm a little dumbfounded by the following problem I seem to be
| encountering when using the select.select module. The code basically
| looks like the following:
|
| class RequestHandler(SocketServer.BaseRequestHandler):
|     def handle():
|
| 	# Read in command and data length in blocking mode.
| 	command, length = ...
|
| 	# Set nonblocking and initialize data.
|         self.request.setblocking(0)
| 	data = ""
|
| 	# Read until data has been completely read
| 	while len(data) < length:
| 	    (rlist,wlist,xlist) = select.select([self.rfile],[],[],5.0)
|
| 	    # Connection dumped.
| 	    if not rlist:
| 		return
|
| 	    # Read piece of data.
| 	    data += self.rfile.read(length-len(data))
|
| 	# Do something with data and command.
| 	...
|
| The funny thing is that the command and the length (which are read in
| standard/blocking mode) are read correctly, but the select call always
| returns an empty list for the read available list. It is just the same
| if I specify the socket directly (self.connection/self.request).
|
| Doing a print self.rfile.read(1) right after the select doesn't block
| and returns the next character correctly (which is there, I am sure of
| that!), even though the rlist is empty! And setting the timeout to zero
| just blocks the process as a whole, although self.rfile _is_ readable...

select can see only data that's readable from the system level
device (the socket, that is.)  Your data is in a file object, C
library buffer in your process' space.  That's my guess.
Python's select function cleverly supports rfile as an input, but
it isn't so clever as to actually be able to tell you if rfile is
readable;  rather, it can only tell you if rfile's device is readable.

If you don't need the file object buffering, then you might as
well use the socket directly, and call the socket recv() method.
If you never use rfile, it won't read any data into its buffer.

	Donn Cave, donn at drizzle.com



More information about the Python-list mailing list