Multithreading

Yigit Turgut y.turgut at gmail.com
Mon Dec 26 18:00:30 EST 2011


On Dec 26, 10:01 pm, Ian Kelly <ian.g.ke... at gmail.com> wrote:
> On Mon, Dec 26, 2011 at 11:31 AM, Yigit Turgut <y.tur... at gmail.com> wrote:
> > I have a loop as following ;
>
> > start = time.time()
> > end = time.time() - start
> >  while(end<N):
> >          data1 = self.chan1.getWaveform()
> >          end = time.time() - start
> >          timer.tick(10)  #FPS
> >          screen.fill((255,255,255) if white else(0,0,0))
> >          white = not white
> >          pygame.display.update()
> >          for i in range(self.size):
> >              end = time.time() - start
> >              f.write("%3.8f\t%f\n"%(end,data1[i]))
>
> > Roughly speaking, this loop displays something at 10 frames per second
> > and writes data1 to a file with timestamps.
>
> > At first loop data1 is grabbed but to grab the second value (second
> > loop) it needs to wait for timer.tick to complete. When I change FPS
> > value [timer.tick()], capturing period (time interval between loops)
> > of data1 also changes. What I need is to run ;
>
> >          timer.tick(10)  #FPS
> >          screen.fill((255,255,255) if white else(0,0,0))
> >          white = not white
> >          pygame.display.update()
>
> > for N seconds but this shouldn't effect the interval between loops
> > thus I will be able to continuously grab data while displaying
> > something at X fps.
>
> > What would be an effective workaround for this situation ?
>
> You essentially have two completely independent loops that need to run
> simultaneously with different timings.  Sounds like a good case for
> multiple threads (or processes if you prefer, but these aren:
>
> def write_data(self, f, N):
>     start = time.time()
>     while self.has_more_data():
>         data1 = self.chan1.getWaveform()
>         time.sleep(N)
>         for i in range(self.size):
>             end = time.time() - start
>             f.write("%3.8f\t%f\n" % (end, data[i]))

Why is there N variable in write_data function ? N is related to
timer.tick(N) which is related to display function ? time.sleep(N)
will pause writing to file for specified amount of time which is
exactly what I am trying to avoid.



More information about the Python-list mailing list