I want to search for pre-defined list of keywords in a given article and increment the score by 1 if keyword is found in article. I want to use multiprocessing since pre-defined list of keyword is very large - 10k keywords and number of article is 100k.
I came across this question but it does not address my question.
I tried this implementation but getting None as result.
keywords = ["threading", "package", "parallelize"]
def search_worker(keyword):
score = 0
article = """
The multiprocessing package also includes some APIs that are not in the threading module at all. For example, there is a neat Pool class that you can use to parallelize executing a function across multiple inputs."""
if keyword in article:
score += 1
return score
I tried below two method but getting three None as result.
Method1:
pool = mp.Pool(processes=4)
result = [pool.apply(search_worker, args=(keyword,)) for keyword in keywords]
Method2:
result = pool.map(search_worker, keywords)
print(result)
Actual output: [None, None, None]
Expected output: 3
I think of sending the worker the pre-defined list of keyword and the article all together, but I am not sure if I am going in right direction as I don't have prior experience of multiprocessing.
Thanks in advance.
if __name__ == '__main__'?