wait until change
Anand Pillai
pythonguy at Hotpop.com
Mon Oct 27 15:50:36 EST 2003
I thought this was fairly straightforward, but when
I tried to write one I figured out that the standard
python Timers cannot be used.
Anyway here is how you would go about doing it.
This piece of code uses a time out as an exit condition,
whereas in the scenario given by the O.P it would be
the file's timestamp verification.
----------------------------------------------------
from threading import Thread
import time
class ModifiedTimer(Thread):
""" Modified timer with a maximum timeout
and repeated calls """
def __init__(self, interval, maxtime, target):
self.__interval = interval
self.__maxtime = maxtime
self.__tottime = 0.0
self.__target = target
self.__flag = 0
Thread.__init__(self, None, 'mytimer', None)
def run(self):
while self.__flag==0:
time.sleep(self.__interval)
self.__target()
self.__tottime += self.__interval
if self.__tottime >= self.__maxtime:
print 'Timing out...'
self.end()
def end(self):
self.__flag=1
class MyTimer:
def __init__(self):
self.__interval = 5.0
def __timerfunc(self):
print 'Hello, this is the timer function!'
def make_timer(self, interval):
self.__interval = interval
self.__timer = ModifiedTimer(self.__interval, 60.0, self.__timerfunc)
self.__timer.start()
if __name__=="__main__":
t=MyTimer()
t.make_timer(10.0)
----------------------------------------------------------------
The code works as expected. I tested on Windows 98
using python 2.3.
-Anand Pillai
pythonguy at Hotpop.com (Anand Pillai) wrote in message news:<84fc4588.0310210106.6da894c1 at posting.google.com>...
> 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