poll of filesystem

Dan Stromberg drsalists at gmail.com
Sat Jul 2 15:30:53 EDT 2011


On Sat, Jul 2, 2011 at 2:40 AM, Belisko Marek <marek.belisko at gmail.com>wrote:

> Hi,
>
> just want to use poll method to get data from /proc file system. Use
> simple code for it. Problem is it seems poll return POLLIN flag but
> when I try to read data it's always empty. Could be a problem changes
> are so fast that print can't print it?
>

If you're wanting to watch when a file grows, you probably should just read
in a loop.  Not that this is likely what you need.

If you're wanting to see when a file changes, you could detect mtime changes
with os.stat.  This isn't 100% precise, but it should be good enough for
most uses of loadavg.  Make sure to put a sleep in your loop, unless your
loop is busy with other things already.

Various non-portable solutions might be good ways to go for you, because
/proc/loadavg isn't portable anyway.  These include uevent, fanotify and
perhaps inotify (there was apparently some discussion of removing the
inotify /proc restriction a while back, not sure if anything happened with
that).  I'm not confident any or all of these will work, but if you're
concerned about efficiency, you should explore them.

If you don't care about efficiency much, the mtime thing is probably the way
to go - there's little question that should work:

python -c 'import os; print(os.stat("/proc/loadavg").st_mtime)'
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20110702/a1aec60b/attachment-0001.html>


More information about the Python-list mailing list