0
votes

Currently I'm working on a Python script that essentially acts as a search tool, where I have a list containing thousands of strings, and I split this list into smaller lists where each of them are sent to a QThread to be searched.

For example, the variable 'items' contains all of the strings. I split this list into smaller lists for every 100,000 items, by doing the following:

split_lists = [items[x:x+100000] for x in range(0, len(items), 100000)]

Next, I send each small list to my QThread worker class to be searched for e.g. a keyword:

keyword = "Hello"

for i in range(len(split_lists)):
  item_searcher = ItemSearcher(split_list[i], keyword)

Essentially, this would speed up the search by searching different parts of the main list concurrently, rather than searching the entire main list in the main thread.

My QThread worker class (ItemSearcher) is as follows:

class ItemSearcher(QtCore.QThread):

def __init__(self, itemList, searchKeyword):
    """
    Constructor
    """
    super().__init__()

    # Initialize fields
    self.item_list = itemList
    self.search_keyword = searchKeyword

def run(self):

   for item in self.item_list:
     if self.search_keyword in item:
       # Item found
       print(item)

As of right now, the script just finishes executing immediately without any output and seems like the QThread is not working as it should. I'm fairly new to working with QThread, so any tips or suggestions on how to get the QThread to work would be much appreciated.

Note that Python's GIL doesn't allow real concurrence, and actual threading makes sense almost only with I/O operations: if you think that using threading would "speed up" text search, be aware that it won't. Besides, in your code you're not starting any thread, and since you're creating the threads in a loop without keeping any reference, the result would be that they will all but the last will be destroyed for every iteration of the loop, and the last will be destroyed as well as soon as the function that contains that loop returns. - musicamante
Why don't you use a dict? - ניר
what is ItemSearcher? - ניר