select.poll.poll() never blocks

Rhodri James rhodri at wildebst.demon.co.uk
Wed Feb 11 22:47:46 EST 2009


On Thu, 12 Feb 2009 03:01:16 -0000, birdsong <david.birdsong at gmail.com>  
wrote:

> So I guess I didn't have a complete understanding of poll, I thought
> it returned file descriptors that had registered an event that the
> user asked to watch for.  In my case, select.POLLIN
> Constant 	Meaning
> POLLIN 	There is data to read
>
> ...there ins't any data to be read.  The file is readable, yes, but is
> that all that the syscall has to offer?

To quote the web page referenced in help(select):

"It cannot be used on regular files to determine whether a file has grown  
since it was last read."

poll() and friends are wrappers over the socket library "poll" and
"select" functions, which primarily exist to facilitate asynchronous
socket communications.  They can take ordinary file descriptors as
well as sockets because there's no good reason not to, but a regular
file will always be ready to read as long as you haven't read past
the end of file.

You're trying to use a regular file as if it was a pipe.  My first
obvious question is do you have to do it this way?  Do you have
any control over the program writing to file?  Can you cause the
output to go to stdout and pipe it into your script's stdin
instead?  That would make your life vastly easier.

If you can't, I'm not sure what your best strategy is.  I'd
be tempted to use "tail -f filetocheck | yourscript.py" and
palm the job off on an already-written tool.  If you want to
do it in Python, the only thing that springs to mind is
periodically checking the size of the file and reading more
when that changes.  You'll need to be very careful to keep
what size you think the file is in sync with how much you've
read!

-- 
Rhodri James *-* Wildebeeste Herder to the Masses



More information about the Python-list mailing list