Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #17974

Re: Multithreading

References <695ebd75-e0ff-409a-9f9a-8143b78bee0a@d10g2000vbh.googlegroups.com>
From Ian Kelly <ian.g.kelly@gmail.com>
Date 2011-12-26 13:01 -0700
Subject Re: Multithreading
Newsgroups comp.lang.python
Message-ID <mailman.4106.1324929718.27778.python-list@python.org> (permalink)

Show all headers | View raw


On Mon, Dec 26, 2011 at 11:31 AM, Yigit Turgut <y.turgut@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]))

def write_data_with_display(self, f, N, X):
    thread = threading.Thread(target=self.write_data, args=(f, N))
    thread.start()
    white = False
    while thread.is_alive():
        timer.tick(X)
        screen.fill((255, 255, 255) if white else (0, 0, 0))
        white = not white
        pygame.display.update()

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Multithreading Yigit Turgut <y.turgut@gmail.com> - 2011-12-26 10:31 -0800
  Re: Multithreading Ian Kelly <ian.g.kelly@gmail.com> - 2011-12-26 13:01 -0700
    Re: Multithreading Yigit Turgut <y.turgut@gmail.com> - 2011-12-26 15:00 -0800
  Re: Multithreading Ian Kelly <ian.g.kelly@gmail.com> - 2011-12-26 13:03 -0700
    Re: Multithreading Yigit Turgut <y.turgut@gmail.com> - 2011-12-26 12:13 -0800
      Re: Multithreading Ian Kelly <ian.g.kelly@gmail.com> - 2011-12-26 13:39 -0700

csiph-web