i have one problem with my code in python3 : i tried many solutions but still the same result.
import pprint
import concurrent
import multiprocessing
from concurrent import futures
class exempleStackOverFlow:
def __init__(self):
self.dict1={}
self.headers = {"header1","header2","header3"}
def fillIn(self,key):
response = {key:{'first':['1','2','3']}}
self.dict1.update(response)
pprint.pprint(response)
e = exempleStackOverFlow()
def try_my_operation(item):
try:
e.fillIn(item)
except:
print('error with item')
executor = concurrent.futures.ProcessPoolExecutor(2)
futures = [executor.submit(try_my_operation, item) for item in e.headers]
concurrent.futures.wait(futures)
print(e.dict1)
Result
{'header3': {'first': ['1', '2', '3']}}
{'header1': {'first': ['1', '2', '3']}}
{'header2': {'first': ['1', '2', '3']}}
{}
Expected
{'header3': {'first': ['1', '2', '3']}}
{'header1': {'first': ['1', '2', '3']}}
{'header2': {'first': ['1', '2', '3']}}
{'header1': {'first': ['1', '2', '3']},
'header2': {'first': ['1', '2', '3']},
'header3': {'first': ['1', '2', '3']}}
(or what ever order of headers)
I might not have understood how this is working, i would like some light on my problem please, the dict1 is empty at the end when i enter
try_my_operation
.
it does update well without the multiprocessing/concurrent ( like if i do e.fillIn("headers1"),e.fillIn("headers2")
... and after i can get the dict1 full.
Thank you for any future comments