Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ProcessPoolExecutor pass multiple arguments

Tags:

python

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'

like image 415
johnrao07 Avatar asked Mar 04 '23 03:03

johnrao07


2 Answers

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))
like image 197
salparadise Avatar answered Mar 08 '23 06:03

salparadise


EDIT:

With multiprocessing Pool you would use starmap() and it would use start * to unpack tuple to arguments

ESPNPlayerFree( *('277906', 'cA2i150s81HI3qbq1fzi', 'za1Oq5CGHj3pkkXWNghG') ) 
ESPNPlayerFree( *('213674', 'cA2i150s81HI3qbq1fzi', 'za1Oq5CGHj3pkkXWNghG') ) 

It seems concurrent.futures.ProcessPoolExecutor doesn't have starmap() so it sends it as one argument - tuple

ESPNPlayerFree( ('277906', 'cA2i150s81HI3qbq1fzi', 'za1Oq5CGHj3pkkXWNghG') ) 
ESPNPlayerFree( ('213674', 'cA2i150s81HI3qbq1fzi', 'za1Oq5CGHj3pkkXWNghG') ) 

and you would need to unpack it inside function

def __init__(self, data):
    player_id, match_id, match_id_team = data
like image 27
furas Avatar answered Mar 08 '23 08:03

furas