wait until change

Anand Pillai pythonguy at Hotpop.com
Tue Oct 21 05:06:19 EDT 2003


If you dont want to use sleep(), try this mechanism
based on 'Timer' class in the threading module. 

1. Create a timer thread that calls a function
   which checks for the time stamp using os.stat().
2. Schedule the timer to wake up after 'n' seconds
   and call this function.
3. If the call succeeds all and well, otherwise in
   the function, create a new timer and set it again
   to wake itself up after another 'n' seconds.

A timer can be used only once so you need to make a new timer
for every wake-up call. Perhaps not very good on memory,
but it might save you the CPU cycles.

You can wrap the whole thing in a class with the timer
bound to a local class variable, and the function as
another class attribute.

HTH.

-Anand

Tom <llafba_NOSPAM_ at gmx.net> wrote in message news:<bmpcos$2f0$1 at news.uni-kl.de>...
> Lane LiaBraaten wrote:
> 
> >I would use os.stat() which returns all sorts of info about the file including 
> >modification time.
> >
> >More info at: http://web.pydoc.org/1.5.2/os.html
> >
> >time=os.stat(file)[8]
> >
> ># analyze data
> >
> >while 1:
> >	if os.stat(file)[8]>time:
> >		#analyze data again
> >  
> >
> Hi Lane,
> 
> thanks for your answer it works great and gives me a lot of new ideas 
> that I can work with.
> That "while 1" command looks interessting. I never used while that way. 
> The Library Reference doesn't explain it. But am I right if I assume 
> that this is  a loop that continues forever? Despite using break of 
> course. I ask, because your suggestion works, but unfortunately uses 
> 100% of my CPU power. :-( Well, it works, but is there maybe another way 
> to do the same without using so much of my resources? :-) That would be 
> great.
> 
> Thank's again.
> Tom




More information about the Python-list mailing list