我有一个需要在 n 个线程中处理的列表。我写了这个脚本,但它不正确。这里的线程数将等于列表中的条目数。但我不知道如何制作n个线程
import codecs
import os
import threading
def pr(str):
print(str + threading.currentThread().getName())
def soap(id_number):
for number in id_number:
t = threading.Thread(target=pr, args=(number,))
t.start()
if __name__ == '__main__':
id_file = codecs.open(os.path.join(os.path.dirname(os.path.realpath(__file__)), "card.txt"), "r", encoding='utf-8')
lines = id_file.readlines()
id_file.close()
soap(lines)
exit(0)
将列表分成几部分并将其传递给流: