Here is a minimum example of my problem:
import concurrent.futures
from functools import partial
# Object class
class obj:
def __init__(self,tup):
self.tup = tup
# Function includes attributes in objects of class above
def new(fi,fdic):
fdic[fi].new = 'work_'+ str(fdic[fi].tup)
# Dictionary full of instances of obj above
dic = {'a':obj(1),
'b':obj(2),
'c':obj(3),
'd':obj(4),
'e':obj(5),
'f':obj(6),
}
partial_new = partial(new, fdic=dic)
Now I want to multiprocess all the objects in the dictionary (because I have too many in reality). The code below runs. But it does not "work", because I actually need ProcessPool (I think? Because I want to process things in parallel).
with concurrent.futures.ThreadPoolExecutor() as executor:
for _ in executor.map(partial_new, dic.keys()):
pass
print(dic['b'].new)
This one does not run:
with concurrent.futures.ProcessPoolExecutor() as executor:
for _ in executor.map(partial_new, dic.keys()):
pass
print(dic['b'].new)
My question is: How do I make this work?
I just need to use the function to modify all the objects inside the dictionary in parallel. Later I wills save the full dictionary, but the function that I apply does not return anything (if this makes things easier).