Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Future raising TypeError after wait

I'm trying to optimise an expensive operation in some existing code using parallel processing. I've used concurrent.futures to do so in the past but only when they didn’t return anything.

This time I want to marshall the results, but when printing my collection I'm getting every future's status as something like <Future at 0x... state=finished raised TypeError>. Can anyone explain what I'm doing wrong?

import concurrent.futures

with concurrent.futures.ProcessPoolExecutor() as executor:

  def _future(self) -> None:
    print("here")

  futures = []
  for number in list(range(0,100)):
    future = executor.submit(_future)
    futures.append(future)

  finished = concurrent.futures.wait(futures, 5)
  print(finished)
like image 606
Robin Whittleton Avatar asked Mar 23 '19 15:03

Robin Whittleton


1 Answers

Your _future function takes one parameter, and yet your executor.submit is passing no argument to it. You should pass, for example, number, as an argument to it instead:

for number in list(range(0,100)):
    future = executor.submit(_future, number)
    futures.append(future)

On the other hand, since you're naming _future's one parameter as self, it implies you intend it to be an instance of a class, in which case you should pass to it the proper instance object in your original, non-minimized code.

like image 198
blhsing Avatar answered Nov 07 '22 22:11

blhsing