if __name__ == '__main__':
MATCH_ID = str(doc_ref2.id)
MATCH_ID_TEAM = doc_ref3.id
with concurrent.futures.ProcessPoolExecutor(max_workers=30) as executor:
results = list(executor.map(ESPNPlayerFree, teamList1))
MATCH_ID_TEAM = str(doc_ref4.id)
with concurrent.futures.ProcessPoolExecutor(max_workers=30) as executor:
results = list(executor.map(ESPNPlayerFree, teamList2))
When I print the MATCH_ID_TEAM
it prints the value. But in the process, it shows up the default value which I set empty at the top.
How do I update the value of my variables to all the processes?
ESPNPlayerFree is a class that takes `id` as an argument. So `teamList1` and `teamList2` are list of ids to initialize my objects.
MATCH_ID
and MATCH_ID_TEAM
are variables that are used in my Class ESPNPlayerFree
OS Windows 10 64bit
IDE Pycharm
Python Version 3.6.1
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
andMATCH_ID_TEAM
to remain the same for a whole job, that is one call toexecutor.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 useszip()
to combine items from all iterables as arguments.Output:
For the second job, with new
MATCH_ID_TEAM
you then don't have to recreate theProcessPoolExecutor
again, you just use the existing again by staying within the context-manager as long as you need it.