I am quite new to python/PyQt so diving into the deep end with a worker thread. I have code as below that I use to run functions on a worker thread, leaving the main thread free. My worker thread looks as below:
from PyQt4 import QtCore
class my_worker_thread(QtCore.QThread):
'''
a worker thread
'''
def __init__(self,function, *args, **kwargs):
QtCore.QThread.__init__(self)
self.exiting=False
self.function=function
self.args=args
self.kwargs=kwargs
def __del__(self):
self.exiting=True
self.wait()
def run(self):
self.function(*self.args,**self.kwargs)
return
In this case I am spawning many,many workers to process files(about 1000 txt files) the main thread reads single parent file which lists all the files that need to be read. for each line in the parent file I spawn a worker thread, so that the file can be processed by the worker. The parent may have the same file in more than one line, so a worker may have to process a file more than once.
The calling logic is like so:
def main_caller(self):
#For each line in parent file spawn a thread to process the file
for line in parent_file:
process_file=my_worker_thread(self.get_file_information,line)
process_file.start()
So far it kind of works, but I do get the following error, which crashes the program
QThread:Thread has to wait on itself
QThread:Destoryed while thread is still running
I want to understand what I am doing wrong.
Firstly how do I get the worker thread to exit safely once it has finished its work?
When does the del function in the worker get called?
Should I not have an exit() somewhere? or a quit()
exit worker now that all the work has been done
worker.exit()
I am not sure that the thread is being cleanly terminated, I would appreciate any suggestions on how to use a generic worker that accepts any simple function, execute the function, return and stop cleanly
I am using PyQt4 on Windows if that makes a difference.