【发布时间】:2014-03-19 04:58:41
【问题描述】:
您好,我正在尝试运行文档中的多处理示例:http://docs.python.org/3.4/library/concurrent.futures.html,使用素数但差异很小。
我希望能够调用具有多个参数的函数。我正在做的是将小段文本(在大约 30k 长的列表中)匹配到更大的一段文本,并返回较大字符串中较小字符串的开始位置。
我可以像这样连续执行此操作:
matchList = []
for pattern in patterns:
# Approximate pattern matching
patternStartingPositions = processPattern(pattern, numMismatchesAllowed, transformedText, charToIndex, countMatrix, firstOccurrence, suffixArray)
# Now add each starting position found onto our master list.
for startPos in patternStartingPositions:
matchList.append(startPos)
但我想这样做是为了加快速度:
matchList = []
with concurrent.futures.ProcessPoolExecutor() as executor:
for pattern, res in zip(patterns, executor.map(processPattern(pattern, numMismatchesAllowed, transformedText, charToIndex, countMatrix, firstOccurrence, suffixArray), patterns)):
print('%d is starts at: %s' % (pattern, res))
在这个阶段,我刚刚收到了 print 调用,因为我无法获得上面的行,即进程的调用工作。
我想要做的和示例代码之间唯一真正的区别是我的函数需要 7 个参数,但我不知道该怎么做,花了半天时间。
上面的调用会产生这个错误:
UnboundLocalError:赋值前引用了局部变量“模式”。
这是有道理的。
但是,如果我忽略第一个参数,它会随着每次调用而改变,并忽略 processPattern 函数的第一个参数:
matchList = []
with concurrent.futures.ProcessPoolExecutor() as executor:
for pattern, res in zip(patterns, executor.map(processPattern(numMismatchesAllowed, transformedText, charToIndex, countMatrix, firstOccurrence, suffixArray), patterns)):
print('%d is starts at: %s' % (pattern, res))
然后我得到这个错误:
TypeError: processPattern() 缺少 1 个必需的位置参数:'suffixArray'。
我不知道如何在调用中获取pattern 参数!
【问题讨论】:
标签: python-3.x multiprocessing argument-passing