-1

Hi I am working on forking the GAssistPi project since the developer is no longer actively working on it. There appears to be an issue with how hes handling threads. I am new to python so I probably can't explain it properly.

The code base is huge. it contains many libraries and is written for Linux. However here is the code which only includes the bits I think are relavent:

class Myassistant():

    def __init__(self):
        self.event = threading.Event
        self.t1 = Thread(target=self.picovoice_run)
        if GPIOcontrol:
            self.t2 = Thread(target=self.pushbutton)
        if configuration['MQTT']['MQTT_Control']=='Enabled':
            self.t3 = Thread(target=self.mqtt_start)
        if irreceiver!=None:
            self.t4 = Thread(target=self.ircommands)
        if configuration['ADAFRUIT_IO']['ADAFRUIT_IO_CONTROL']=='Enabled':
            self.t5 = Thread(target=self.adafruit_mqtt_start)

So my question is how do I safely shutdown these threads?

Due how the project is currently configured and coded, only the t2 thread is started. And evidently from not being able to quit the python process by pressing ctrl c once, it's not being shutdown safely.

I tried following this tutorial https://superfastpython.com/stop-a-thread-in-python/ However at first it gave an error saying ERROR : set() missing 1 required positional argument: 'self' when I called assistant.event.set() . The print statement proves there is an instance of threading.Event class

if __name__ == '__main__':
    try:        
         assistant = Myassistant()
         assistant.main()
         
         print(assistant.event)
         assistant.event.set()
         y = assistant.event.is_set()
         print(y)
Ageis
  • 2,221
  • 4
  • 22
  • 34

1 Answers1

0

simply used a global variable because in my case only the main thread has write access to the global variable. So I didn't need to worry about thread safety,

In main.py script I have

if __name__ == '__main__':
    try:        
         exiting = False
         assistant = Myassistant()
         assistant.main()
         exiting = True

Then in each of the thread target methods I have

def example()
   global exiting
   while True:
     if exiting:
       break
Ageis
  • 2,221
  • 4
  • 22
  • 34