select.select() on Regular Files?

Jim Dennis jimd at vega.starshine.org
Wed Mar 6 14:11:23 EST 2002


 I'm trying to figure how to write a daemon that will
 efficiently wait for activity on any of several file and/or
 socket descriptors.  Naturally I'm trying to use the select()
 function.

 Here's a skeleton script:

#!/usr/bin/env python2.2
## select on a regular file?
## JimD <jimd at starshine.org>: Sat Mar  2 21:21:55 PST 2002
import select, time
if __name__ == '__main__': 
	import sys
	x=0
	f = open(sys.argv[1], "rb")
	f.seek(-1,2) 
	while 1:
		readfd = [f]
		line = ""
		r,w,e = select.select(readfd,[],[], 120)
		if r[0] is not None:
			line = r[0].read()
			print x, line,
			x += 1

 Unfortunately it continually outputs.  I don't understand why
 if there's no more data at the end of the file.  Is my read()
 not pushing my file position all the way to the EOF?  It's as
 though there's *always* data available on that file descriptor.

 This seems to contradict the documentation:

>>> print select.select.__doc__
select(rlist, wlist, xlist[, timeout]) -> (rlist, wlist, xlist)

Wait until one or more file descriptors are ready for some kind of I/O.
The first three arguments are lists of file descriptors to be waited
for:

 ...

*** IMPORTANT NOTICE ***
On Windows, only sockets are supported; on Unix, all file descriptors.
>>>


 Is this a bug?  I'm using Python2.2 as packaged by Debian (unstable)
 on a Debian GNU/Linux system  (what a surprise, a Debian package 
 on a Debian system!) under 2.4.9 SMP kernel).

 Would poll() be a better choice?  The __doc__ for that is a bit
 terse.
 
>>> print select.poll.__doc__  
Returns a polling object, which supports registering and
unregistering file descriptors, and then polling them for I/O events.
>>> 





More information about the Python-list mailing list