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


Groups > comp.lang.python > #17969 > unrolled thread

Multithreading

Started byYigit Turgut <y.turgut@gmail.com>
First post2011-12-26 10:31 -0800
Last post2011-12-26 13:39 -0700
Articles 6 — 2 participants

Back to article view | Back to comp.lang.python


Contents

  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

#17969 — Multithreading

FromYigit Turgut <y.turgut@gmail.com>
Date2011-12-26 10:31 -0800
SubjectMultithreading
Message-ID<695ebd75-e0ff-409a-9f9a-8143b78bee0a@d10g2000vbh.googlegroups.com>
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 ?

[toc] | [next] | [standalone]


#17974

FromIan Kelly <ian.g.kelly@gmail.com>
Date2011-12-26 13:01 -0700
Message-ID<mailman.4106.1324929718.27778.python-list@python.org>
In reply to#17969
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()

[toc] | [prev] | [next] | [standalone]


#17992

FromYigit Turgut <y.turgut@gmail.com>
Date2011-12-26 15:00 -0800
Message-ID<560bea15-6abc-45ed-ac7a-cf23ff44f130@e2g2000vbb.googlegroups.com>
In reply to#17974
On Dec 26, 10:01 pm, Ian Kelly <ian.g.ke...@gmail.com> wrote:
> On Mon, Dec 26, 2011 at 11:31 AM, Yigit Turgut <y.tur...@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.

[toc] | [prev] | [next] | [standalone]


#17975

FromIan Kelly <ian.g.kelly@gmail.com>
Date2011-12-26 13:03 -0700
Message-ID<mailman.4107.1324929816.27778.python-list@python.org>
In reply to#17969
On Mon, Dec 26, 2011 at 1:01 PM, Ian Kelly <ian.g.kelly@gmail.com> wrote:
> 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:

I accidentally sent before I was finished.  I was saying "or processes
if you prefer, but these aren't CPU-bound, so why complicate things?"

Cheers,
Ian

[toc] | [prev] | [next] | [standalone]


#17976

FromYigit Turgut <y.turgut@gmail.com>
Date2011-12-26 12:13 -0800
Message-ID<dfb394b4-e808-428c-91c7-89bb0fcabe88@i8g2000vbh.googlegroups.com>
In reply to#17975
On Dec 26, 10:03 pm, Ian Kelly <ian.g.ke...@gmail.com> wrote:
> On Mon, Dec 26, 2011 at 1:01 PM, Ian Kelly <ian.g.ke...@gmail.com> wrote:
> > 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:
>
> I accidentally sent before I was finished.  I was saying "or processes
> if you prefer, but these aren't CPU-bound, so why complicate things?"
>
> Cheers,
> Ian

I had thought the same workaround but unfortunately loop is already
under a def ;

def writeWaveform(self, fo, header=''):
        data1 = numpy.zeros(self.size)
        screen = pygame.display.set_mode((0, 0), pygame.FULLSCREEN)
        timer = pygame.time.Clock()
        white = True
        fo.write(header)
        start = time.time()
        end = time.time() - start
        while(end<10):
	  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]))

[toc] | [prev] | [next] | [standalone]


#17977

FromIan Kelly <ian.g.kelly@gmail.com>
Date2011-12-26 13:39 -0700
Message-ID<mailman.4108.1324932022.27778.python-list@python.org>
In reply to#17976
On Mon, Dec 26, 2011 at 1:13 PM, Yigit Turgut <y.turgut@gmail.com> wrote:
> I had thought the same workaround but unfortunately loop is already
> under a def ;

So nest the functions, or refactor it.  Either way, that shouldn't be
a significant obstacle.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web