'ProcessPoolExecutor pass multiple arguments

ESPNPlayerFree

class ESPNPlayerFree:

def __init__(self, player_id, match_id, match_id_team):
...

teamList1:

 [('277906', 'cA2i150s81HI3qbq1fzi', 'za1Oq5CGHj3pkkXWNghG'), ('213674', 'cA2i150s81HI3qbq1fzi', 'za1Oq5CGHj3pkkXWNghG')]

Code

 with concurrent.futures.ProcessPoolExecutor(max_workers=30) as executor:
    results = list(executor.map(ESPNPlayerFree, teamList1))

TypeError: __init__() missing 2 required positional arguments: 'match_id' and 'match_id_team'



Solution 1:[1]

Since starmap is not available, create a helper factory function for this:

def player_helper(args):
    return ESPNPlayerFree(*args)

with concurrent.futures.ProcessPoolExecutor(max_workers=30) as executor:
    results = list(executor.map(player_helper, teamList1))

Or make it into a classmethod for class ESPNPlayerFree if it makes more sense:

class ESPNPlayerFree:

...

@classmethod
def expand_args(cls, args):
    return cls(*args)


with concurrent.futures.ProcessPoolExecutor(max_workers=30) as executor:
    results = list(executor.map(ESPNPlayerFree.expand_args, teamList1))

Solution 2:[2]

According to source code of Executor.map:

fs = [self.submit(fn, *args) for args in zip(*iterables)]

So you can have:

executor.map(ESPNPlayerFree, player_id_list, match_id_list, match_id_team_list)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 salparadise
Solution 2 sunrimii