我之前在python中使用过多线程,但无论出于何种原因,我似乎无法找到一个有效的解决方案来创建适合我需要的正常工作脚本。在我编写的这个脚本中,我有两个列表,我试图合并并操作到一个列表中。我正在以预定数量的行和#34; 1,000,000行"将列表写入文本文件。
进行多线程处理时出现的问题是变量i用于计算列表中的行" text_amount"没有正确计算它们。并且在第二次for循环的第一次旋转后回到零。我不确定这是否是基于逻辑的错误,我错误地使用全局变量,或者我需要使用另一种线程方法。非常感谢你们的任何反馈。
这是我没有多线程的脚本:
thelist = []
text_amount = 0
filenumber = 0
for text2 in file2:
for text1 in file1:
if both word fit certain criteria:
word = text1 + text2
thelist.extend(word)
text_amount += 1
if text_amount >= 1000000:
filenumber += 1
text_amount = 0
print ("Saved File " + str(filenumber))
outputName = str(filenumber)
savefilefunction(outputName, thelist)
thelist = []
#amends the last unsaved lines
filenumber += 1
outputName = str(filenumber)
savefilefunction(outputName, thelist)
以下是我对多媒体阅读的尝试:
def together(text1,text2):
global filenumber
global text_amount
global thelist
if both word fit certain criteria:
word = text1 + text2
thelist.extend(word)
text_amount += 1
if filenumber >= 1000000:
filenumber += 1
text_amount = 0
print("Saved File " + str(text_amount))
outputName = outName + "-" + str(text_amount) + ".txt"
savefilefunction(outputName, thelist)
thelist = []
global fileNameNumber
global fileNumber
global keywordList
thelist = []
text_amount = 0
filenumber = 0
for text2 in file2:
for text1 in file1:
with ProcessPoolExecutor(max_workers=5) as executor:
future_results = {executor.submit(together, text1,text2): text1 for text1 in file1}
for future in concurrent.futures.as_completed(future_results):
future.result()
filenumber += 1
outputName = str(filenumber)
savefilefunction(outputName, thelist)