Multithreading python,two tkinter windows

Chris Angelico rosuav at gmail.com
Sun Nov 1 09:31:35 EST 2015


On Mon, Nov 2, 2015 at 1:05 AM, Vindhyachal Takniki
<vindhyachal.takniki at gmail.com> 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



More information about the Python-list mailing list