How to use two threads (GUI and backend)

pozz pozzugno at gmail.com
Wed Oct 26 06:22:53 EDT 2016


Il 26/10/2016 09:13, pozz ha scritto:
 > [...]
> When the user press Start button (the pressed handler is in the GUI class):
>
>   self.comm_active = True
>   threading.Thread(target=self.comm_thread).start()
>
> The backend thread is:
>
>   def comm_thread(self):
>     while self.comm_active:
>       self.device.get_status()
>       GLib.idle_add(self.polling_received)
>       time.sleep(1)
>     self.m.close()
 > [...]


Now I have some concerns even in using self.comm_active.  It is a 
boolean variable accessed by the GUI thread (inside Start/Stop buttons 
handler) and backend thread (in the "while self.comm_active" instruction).

Is it safe to access this variable from two different threads?  Should I 
implement a safer and more complex mechanism?  If yes, what mechanism?




More information about the Python-list mailing list