Other posters have covered possible solutions, but to explain your error, you should be passing the function and parameters as separate objects to executor.map
. Here is the example from the docs
with concurrent.futures.ProcessPoolExecutor() as executor:
# is_prime is the function, PRIMES are the arguments
for number, prime in zip(PRIMES, executor.map(is_prime, PRIMES)):
print('%d is prime: %s' % (number, prime))
Your code is evaluating the processPattern
function and passing in the result to executor.map
with concurrent.futures.ProcessPoolExecutor() as executor:
for pattern, res in zip(patterns, executor.map(processPattern(numMis... # <- BAD
print('%d is starts at: %s' % (pattern, res))
Instead it should be
with concurrent.futures.ProcessPoolExecutor() as executor:
for pattern, res in zip(patterns, executor.map(processPattern, <stuff>)):
print('%d is starts at: %s' % (pattern, res))
Where <stuff>
is an iterable of the arguments to pass to processPattern
on each subsequent call.
Or, seeing as the other args stay fixed, create a function that just takes the one parameter you are iterating over and pass in patterns
as the iterable (as @uhbif19 suggests)
EDIT:
To expand on how to create the <stuff>
iterable, you need an iterable for each argument required by your function (processPattern
in this case). You already have patterns
as the iterable for argument one, if the others are constant then itertools.repeat
may be helpful:
from itertools import repeat
args = (patterns,
repeat(numMismatchesAllowed, len(PATTERNS)),
repeat(transformedText, len(PATTERNS)),
repeat(charToIndex, len(PATTERNS)),
<etc...>
)
Then
for pattern, res in zip(PATTERNS, executor.map(process, *args)):
I include this for the sake of understanding, but you can see how messy this is. The other answers offer better solutions.
EDIT 2:
Here's an example that better illustrates the use of submit vs map
import concurrent.futures
def process(a, b):
return a.upper() + b
with concurrent.futures.ProcessPoolExecutor() as executor:
for c, fut in [(c, executor.submit(process, c, 'b')) for c in 'testing']:
print(c, fut.result())
with concurrent.futures.ProcessPoolExecutor() as executor:
for c, res in zip('testing', executor.map(process, 'testing', 'bbbbbbb')):
print(c, str(res))