Multithreading

Y

Yigit Turgut

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))

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 ?
 
I

Ian Kelly

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))

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))

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()
 
I

Ian Kelly

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
 
Y

Yigit Turgut

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

Ian Kelly

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.
 
Y

Yigit Turgut

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))

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))


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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,990
Messages
2,570,211
Members
46,796
Latest member
SteveBreed

Latest Threads

Top