Multithreading
Ian Kelly
ian.g.kelly at gmail.com
Mon Dec 26 15:01:20 EST 2011
On Mon, Dec 26, 2011 at 11:31 AM, Yigit Turgut <y.turgut 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]))
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()
More information about the Python-list
mailing list