How to use two threads (GUI and backend)

Antoon Pardon antoon.pardon at rece.vub.ac.be
Wed Oct 26 07:27:02 EDT 2016


Op 26-10-16 om 12:22 schreef pozz:
> 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?

Accessing from multiple thread shouldn't be a problem. As long as you only change
it in one thread.





More information about the Python-list mailing list