Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.018 X-Spam-Evidence: '*H*': 0.96; '*S*': 0.00; 'essentially': 0.10; 'am,': 0.12; 'def': 0.13; '(0,': 0.16; '(second': 0.16; '0))': 0.16; 'capturing': 0.16; 'frames': 0.16; 'workaround': 0.16; '\xa0for': 0.16; 'cc:addr:python-list': 0.16; 'mon,': 0.16; 'wrote:': 0.18; 'cc:no real name:2**0': 0.20; 'seconds': 0.21; 'dec': 0.22; '(or': 0.22; 'header:In-Reply-To:1': 0.22; 'thus': 0.23; "shouldn't": 0.23; 'cc:2**0': 0.24; 'skip:[ 10': 0.27; 'effect': 0.28; 'message-id:@mail.gmail.com': 0.28; 'cc:addr:python.org': 0.29; 'second': 0.29; 'threads': 0.30; 'skip:\xa0 30': 0.32; 'loop': 0.34; 'received:209.85.212': 0.34; '8bit%:3': 0.34; 'changes.': 0.34; 'something': 0.35; 'file': 0.36; 'thread': 0.37; 'two': 0.37; 'but': 0.37; 'run': 0.37; 'received:google.com': 0.37; 'received:209.85': 0.38; 'skip:\xa0 10': 0.39; 'else': 0.39; 'received:209': 0.40; '2011': 0.61; 'effective': 0.61; 'grab': 0.66; '26,': 0.67; 'roughly': 0.67; '11:31': 0.84; 'x):': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc:content-type:content-transfer-encoding; bh=+F1atvLlDznYNO8vLDYNg1WPzVPfZ92jzAQQQBxkyBY=; b=OGmVybwEVOCv68VOZuF/O1xuyB9mW1l4lazZq3mZDlIg8BuTaRLwG9ybmUN9T0HQ7V VyPB8L274EG/1P9KzsBDu+AJLn+uKp2PTWDdJEtM4hXNVnzKoYucUmH4a6zQk1jvurqJ JQDrD2MZdQO0J92kZaPg3l0XP/tV9rV9O/zpg= MIME-Version: 1.0 In-Reply-To: <695ebd75-e0ff-409a-9f9a-8143b78bee0a@d10g2000vbh.googlegroups.com> References: <695ebd75-e0ff-409a-9f9a-8143b78bee0a@d10g2000vbh.googlegroups.com> From: Ian Kelly Date: Mon, 26 Dec 2011 13:01:20 -0700 Subject: Re: Multithreading To: Yigit Turgut Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Cc: python-list@python.org X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 57 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1324929718 news.xs4all.nl 6951 [2001:888:2000:d::a6]:50567 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:17974 On Mon, Dec 26, 2011 at 11:31 AM, Yigit Turgut wrote: > I have a loop as following ; > > start =3D time.time() > end =3D time.time() - start > =A0while(end =A0 =A0 =A0 =A0 =A0data1 =3D self.chan1.getWaveform() > =A0 =A0 =A0 =A0 =A0end =3D time.time() - start > =A0 =A0 =A0 =A0 =A0timer.tick(10) =A0#FPS > =A0 =A0 =A0 =A0 =A0screen.fill((255,255,255) if white else(0,0,0)) > =A0 =A0 =A0 =A0 =A0white =3D not white > =A0 =A0 =A0 =A0 =A0pygame.display.update() > =A0 =A0 =A0 =A0 =A0for i in range(self.size): > =A0 =A0 =A0 =A0 =A0 =A0 =A0end =3D time.time() - start > =A0 =A0 =A0 =A0 =A0 =A0 =A0f.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 ; > > =A0 =A0 =A0 =A0 =A0timer.tick(10) =A0#FPS > =A0 =A0 =A0 =A0 =A0screen.fill((255,255,255) if white else(0,0,0)) > =A0 =A0 =A0 =A0 =A0white =3D not white > =A0 =A0 =A0 =A0 =A0pygame.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 =3D time.time() while self.has_more_data(): data1 =3D self.chan1.getWaveform() time.sleep(N) for i in range(self.size): end =3D time.time() - start f.write("%3.8f\t%f\n" % (end, data[i])) def write_data_with_display(self, f, N, X): thread =3D threading.Thread(target=3Dself.write_data, args=3D(f, N)) thread.start() white =3D False while thread.is_alive(): timer.tick(X) screen.fill((255, 255, 255) if white else (0, 0, 0)) white =3D not white pygame.display.update()