i have a problem with threads.
I’m trying to run the search for multimedia files (for example, * .avi, * .mkv, etc.) in a separate thread. The found files should be counted. I use the “The Genric” code from Martin’s book for thread execution.
The function performed in the thread looks like this.:
@staticmethod def count_files(f_dir, name_filters, filters, progress_callback): """ Counts how many files match the search pattern :param f_dir: root directory for search :param name_filters: filter for filenames (e.g. *.avi) :param filters: filtering options available to QDir (e.g. QDir.Files) :param progress_callback: signal for progress function :type f_dir: str :type name_filters: list :type filters: int :type progress_callback: pyqtSignal :return: ammount of processed files :rtype int """ print("In Count Files") try: if f_dir: it_count = QDirIterator(f_dir, name_filters, filters, QDirIterator.Subdirectories) # count files while it_count.hasNext(): it_count.next() FdbCheckFiles.__processed_files += 1 progress_callback.emit(FdbCheckFiles.__processed_files) finally: pass return FdbCheckFiles.__processed_files
My problem: In a search on an entire hard disk or partition is not permanently a file found and therefore no signal sent out (progress_callback). Without a signal, the GUI freezes.
How can I fix the problem?
All the examples I’ve gotten about threads in QT work with timers that send pretty regular signals to the mainthread. That always works great.
I also tried to create threads in different ways. Eg. inherit a worker from QObject and control it with “worker.moveToThread ()” and signals. The result is always the same.
Is not it possible to create a thread, let it work and receive only a result signal without blocking the main thread?
In my example run the QDirIterator and only if the iterator is ready to transfer the result?