Anomaly in time.clock()
Gabriel Genellina
gagsl-py2 at yahoo.com.ar
Fri Mar 21 04:03:34 EDT 2008
On 20 mar, 08:47, Godzilla <godzillais... at gmail.com> wrote:
> Thanks Ross and John for your help. I apologise for the code I posted
> earlier not being the full picture of what I was trying to achieve. I
> had instantiated multiple instances of elapseTime class and each of
> them gets called approximately the same time. Below is the updated
> code:
You still have race conditions:
> def setTime(self, state):
> if state == 1:
> self.checkTimeFlag = True
> self.timeStamp = time.clock()
> else:
> self.checkTimeFlag = False
>
> def elapsedTime(self):
> prevTime = time.clock()
> while True:
> curTime = time.clock()
> timeDiff = (curTime - prevTime)
> if timeDiff < 0.0:
> printq.put_nowait('time.clock() has gone backward!!! Time
> Diff '+str(timeDiff))
> if self.checkTimeFlag:
> if (curTime - self.timeStamp) > 1.0:
> printq.put_nowait(self.name+", actual Elapsed
> Time"+str(round(curTime-self.timeStamp, 3)))
> self.checkTimeFlag = False
> prevTime = curTime
> time.sleep(0.05)
Both functions run on different threads, use self.checkTimeFlag and
self.timeStamp.
By example, setTime sets self.checkTimeFlag = True before
self.timeStamp; the other thread can use and old value for
self.timeStamp.
--
Gabriel Genellina
More information about the Python-list
mailing list