ARTICLE AD BOX
following the answer https://stackoverflow.com/a/32138213/7742341 after stopping lightWorker you should to quit from the thread and wait untill it is stopped
def stopLightsThread(self): print('stop lightsThread') self.lightsWorker.stop() self.lightsThread.quit() self.lightsThread.wait()3 Comments
I had to face the same issue in C++, but the problem is the same.
The problem is that your QThread instance is deleted while the associated thread is still running. This can be really dangerous because the thread code execution is interrupted whitout any guarantee that the thread is ready to be deleted.
For example :
a thread control the execution and life time of an object (a worker) a ressource is released in this object destructor (explicity or implicilty like when using QObject parent/child system) since the thread execution is interrupted, the object will not be deletedIt lead to a memory leak and a ressource leak.
In your code, the worker is stopped, but not the working thread. I'm not python expert, but it also seems that your worker object is stopped but not deleted.
To properly stop your worker and thread, you should :
send a message to your worker to tell it to "stop working" ask your thread to quit : it will post an "exit" message to the thread that will be processed after the worker execution wait for your thread to stopThe last step is optionnal : if the thread and worker does not share ressources whith other object, you probably don't need to wait them to finish, just forget about them.
The only execption is that all your thread should be properly stopped before exiting the application : you should wait for all current running threads to stop before application exit.
For simple tasks, you should also consider using QtConcurrent framework.
2 Comments
Thanks this works, using the code Luchko posted. My code no longer throw the error after asing the thread to quit self.lightsThread.quit(). I assume self.lightsThread.wait() is for waiting for the thread to stop, if so why does leaving this out not cause the error to occur in my example code?
2017-04-23T23:30:38.583Z+00:00
Explore related questions
See similar questions with these tags.


