下面是使用线程的示例解决方案。此代码将数据分成相等的块,并将它们用作
compare()
根据我们声明的线程数量。
strings = ("string1", "string2", "string3")
lines = ['some random', 'lines with string3', 'and without it',\
'1234', 'string2', 'string1',\
"string1", 'abcd', 'xyz']
def compare(x, thread_idx):
print('Thread-{} started'.format(thread_idx))
for line in x:
if any(s in line for s in strings):
print("We got one of strings in line: {}".format(line))
print('Thread-{} finished'.format(thread_idx))
穿线部分:
from threading import Thread
threads = []
threads_amount = 3
chunk_size = len(lines) // threads_amount
for chunk in range(len(lines) // chunk_size):
threads.append(Thread(target=compare, args=(lines[chunk*chunk_size: (chunk+1)*chunk_size], chunk+1)))
threads[-1].start()
for i in range(threads_amount):
threads[i].join()
输出:
Thread-1 started
Thread-2 started
Thread-3 started
We got one of strings in line: string2
We got one of strings in line: string1
We got one of strings in line: string1
We got one of strings in line: lines with string3
Thread-2 finished
Thread-3 finished
Thread-1 finished