I'm picking up where @furas left in his comment some days ago. The simplest approach is indeed to pass just everything you need in your class along with .map()
. executor.map()
is expecting iterables, which get zipped to an argument-tuple for each function call to be made in your workers.
You obviously need both MATCH_ID
and MATCH_ID_TEAM
to remain the same for a whole job, that is one call to executor.map()
. Your challenge is that both are iterables (strings), but you need them replicated as a whole and often enough to match with every item of your teamlist-iterable.
So what you do is simply wrap these strings with itertools.repeat()
when you pass them to .map()
together with the team-id list. itertools.repeat()
by default returns an infinite iterator of the passed object. ProcessPoolExecutor
internally then uses zip()
to combine items from all iterables as arguments.
import concurrent.futures
import multiprocessing
from itertools import repeat
class ESPNPlayerFree:
def __init__(self, team_id, match_id, match_id_team):
self.teams_id = team_id
self.match_id = match_id
self.match_id_team = match_id_team
print(
multiprocessing.current_process().name,
self.teams_id, self.match_id, self.match_id_team
)
if __name__ == '__main__':
teams1 = [f"id{i}" for i in range (10)]
teams2 = [f"id{i}" for i in range(10, 20)]
with concurrent.futures.ProcessPoolExecutor(max_workers=4) as executor:
MATCH_ID = 'doc_ref2.id'
MATCH_ID_TEAM = 'doc_ref3.id'
results = list(
executor.map(
ESPNPlayerFree,
teams1,
repeat(MATCH_ID),
repeat(MATCH_ID_TEAM),
)
)
print("--- new MATCH_ID_TEAM ---")
MATCH_ID_TEAM = 'doc_ref4.id'
results = list(
executor.map(
ESPNPlayerFree,
teams2,
repeat(MATCH_ID),
repeat(MATCH_ID_TEAM),
)
)
Output:
ForkProcess-1 id0 doc_ref2.id doc_ref3.id
ForkProcess-2 id1 doc_ref2.id doc_ref3.id
ForkProcess-3 id2 doc_ref2.id doc_ref3.id
ForkProcess-4 id3 doc_ref2.id doc_ref3.id
ForkProcess-1 id4 doc_ref2.id doc_ref3.id
ForkProcess-3 id5 doc_ref2.id doc_ref3.id
ForkProcess-2 id6 doc_ref2.id doc_ref3.id
ForkProcess-4 id7 doc_ref2.id doc_ref3.id
ForkProcess-3 id8 doc_ref2.id doc_ref3.id
ForkProcess-1 id9 doc_ref2.id doc_ref3.id
--- new MATCH_ID_TEAM ---
ForkProcess-1 id10 doc_ref2.id doc_ref4.id
ForkProcess-3 id11 doc_ref2.id doc_ref4.id
ForkProcess-2 id12 doc_ref2.id doc_ref4.id
ForkProcess-4 id13 doc_ref2.id doc_ref4.id
ForkProcess-1 id14 doc_ref2.id doc_ref4.id
ForkProcess-3 id15 doc_ref2.id doc_ref4.id
ForkProcess-2 id16 doc_ref2.id doc_ref4.id
ForkProcess-4 id17 doc_ref2.id doc_ref4.id
ForkProcess-2 id18 doc_ref2.id doc_ref4.id
ForkProcess-1 id19 doc_ref2.id doc_ref4.id
Process finished with exit code 0
For the second job, with new MATCH_ID_TEAM
you then don't have to recreate the ProcessPoolExecutor
again, you just use the existing again by staying within the context-manager as long as you need it.
ESPNPlayerFree
which is huge – SplendentteamLIst1
to list[(MATCH_ID_TEAM, item1), (MATCH_ID_TEAM, item2), etc.]
and use this list with processes. And every process has to unpack argument to variables -match_id_team, item = arg
– Melessathe default value which I set empty at the top.
Please show the code where you (i) set the default value, and (ii) useMATCH_ID_TEAM
inESPNPlayerFree
. – Husain