How to use two threads (GUI and backend)

jmp jeanmichel at sequans.com
Wed Oct 26 07:16:25 EDT 2016


On 10/26/2016 12:22 PM, pozz wrote:
> 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?
>


from http://nedbatchelder.com/blog/201204/two_problems.html

Some people, when confronted with a problem, think, "I know, I'll use 
threads," and then two they hav erpoblesms.


I suggest you write a GUI that make synchronous	calls to a remote 
application, if possible. If the remote app is in python, you have 
access to remote protocols already written for you, Pyro is one of them, 
you can skip the low level communication part.

jm




More information about the Python-list mailing list