Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Chris Angelico Newsgroups: comp.lang.python Subject: Re: Multithreading python,two tkinter windows Date: Mon, 2 Nov 2015 01:31:35 +1100 Lines: 25 Message-ID: References: <271f8b42-da03-40fd-8a8b-c562292577e6@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-Trace: news.uni-berlin.de 1C20I2vYJn6v5PdokClHOgkAf/4mVHCW52zds2uBpODg== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'model,': 0.05; 'subject:two': 0.07; 'cc:addr:python-list': 0.09; 'events.': 0.09; 'thread': 0.10; 'python': 0.10; 'def': 0.13; 'subject:python': 0.14; '"are': 0.16; '1):': 0.16; 'event-driven': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'run.': 0.16; 'still,': 0.16; 'subject:windows': 0.16; 'threads': 0.16; 'true:': 0.16; 'wrote:': 0.16; 'gui': 0.18; '2015': 0.20; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; 'delay': 0.22; 'libraries': 0.22; 'am,': 0.23; 'second': 0.24; 'header:In-Reply-To:1': 0.24; 'mon,': 0.24; 'message-id:@mail.gmail.com': 0.27; 'this.': 0.28; 'cpu': 0.29; 'second,': 0.29; 'thread,': 0.29; 'code': 0.30; 'seconds': 0.31; 'core': 0.32; 'common': 0.33; 'instead,': 0.33; 'received:google.com': 0.35; 'nov': 0.35; 'there': 0.36; 'received:209.85': 0.36; 'subject:: ': 0.37; 'received:209.85.213': 0.37; 'busy': 0.38; 'received:209': 0.38; 'does': 0.39; 'called': 0.40; 'your': 0.60; 'entire': 0.61; 'subject:skip:M 10': 0.72; 'subject:,': 0.82; 'chrisa': 0.84; 'yet?': 0.84; 'to:none': 0.91; 'notion': 0.91 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type; bh=Lbp1rG7xcWyV58VyQqkupBWDFj847AHsncPlNPJmXso=; b=acuQmS+3Y/8JXqpKNNnjFRBAZvRkMbmQgT714oGzrxdsJqeTZxtFt2mysqvVx+ifBg oRXt+duvrHRhq3/R8nPKtUkw5bnYWmierQFsHuc+VSUPHemV0k3RhmryhBh+21aQVmJi D9l1l/Sl+bKTuV6p5Thdfh0wQQTuiZMAopHGZGlz+FHpe81mu95U/5N0gAhWeTqYm1wA uxkswY/9I7ryGxMgojK1dmObho7A0zJoRkcqxovF0IOhSnk1YuO4FhsbxBOxvJBQ68Zh XkK16uQt9tWBXoBplRD583LlJhDDfEFrSLQ9vLmhthmKmfq5vb2GUV8hNkoAGgYCE3yV 37mg== X-Received: by 10.50.66.233 with SMTP id i9mr7156433igt.92.1446388295576; Sun, 01 Nov 2015 06:31:35 -0800 (PST) In-Reply-To: <271f8b42-da03-40fd-8a8b-c562292577e6@googlegroups.com> X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com comp.lang.python:98045 On Mon, Nov 2, 2015 at 1:05 AM, Vindhyachal Takniki wrote: > #get reading at every 1 second > def get_analog_1(thread_name): > global read_ok_1, current_time_1,analog_1 > while True: > if((time.time() - current_time_1) > 1): > if(0 == read_ok_1): > current_time_1 = time.time(); > read_ok_1 = 1; > analog_1 = randint(0,100) Never do this. It's called a "busy-wait", and not only does it saturate your Python thread, it also saturates your entire system - this is going to keep one CPU core permanently busy going "are we there yet? are we there yet?" about the one-second delay. Instead, use time.sleep(), which will delay your thread by one second, allowing other threads to run. Better still, think about your code in terms of events. Most GUI libraries these days are built around an event-driven model, and the notion of "make this event happen 1 second from now" or "10 seconds from now" is a very common one. ChrisA