File Polling (Rereading)

Oliver Fromme olli at haluter.fromme.com
Fri Oct 22 07:19:29 EDT 2004


Daniel Mueller <da_mueller at gmx.net> wrote:
 > Oliver Fromme wrote:
 > > The portable way is to stat the file (see os.stat) every
 > > 10 seconds, look at the mtime (modification time), and
 > > if it did change, then open/read/close.  
 > 
 > hmm i'll have to rephrase my question... i KNOW that the file changes 
 > every 10 seconds...

In that case you have to re-read the file every 10 seconds
anyway, no matter what.

The only optimization that's worthwhile is to keep the file
open all the time, so you spare the overhead of the open()
system call.  In other words, open it _once_, then read it
(do not close it!), and after 10 seconds rewind -- that is,
file.seek(0) -- and re-read it.

I don't know what the contents of that file look like (I
don't use Linux), nor do I want to know.  But since the Li-
nux procfs usually produces ASCII text files (which is a
mistake, in my opinion), I guess it should be pretty easy
to parse and find the difference.

If it's more complicated than that, Python's difflib might
be helpful:  http://docs.python.org/lib/module-difflib.html

Best regards
   Oliver

-- 
Oliver Fromme, Konrad-Celtis-Str. 72, 81369 Munich, Germany

``All that we see or seem is just a dream within a dream.''
(E. A. Poe)



More information about the Python-list mailing list