0

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.

Community
  • 1
  • 1
  • You need to make a collection of the threads you create, and once they are created and started, iterate through them calling `join()`. – quamrana Oct 29 '17 at 09:37
  • @quamrana, I have no idea what join() means in regards to threads, nor how to create a collection of threads. I am really really new to threading,and pyqt, have you got an examples, even links that explain and demonstrate the concepts you're talking about – Man Wa kileleshwa Oct 29 '17 at 09:45
  • So, for example, see my answer here: https://stackoverflow.com/a/46962543/4834 using processes rather than threads, but the principle is the same. Also, you must do your research first. If you have found out about threads, then you must have come across `join()`. – quamrana Oct 29 '17 at 09:55
  • Have a look [here](https://stackoverflow.com/q/14156025/7216865). Keep a reference to the thread in the main thread and kill it. – Maurice Meyer Oct 29 '17 at 10:03

0 Answers0