I have a declared an empty list as global variable. and I assign some value to it. but when I try to do multiprocessing pool the global variable is empty list instead of the assigned value.
from multiprocessing import Process, Pool
camera_data = [{"id": "1", "url": "cam-c.jpg", "area": "1"},
{"id": "2", "url": "cam-d.jpg", "area": "1"},
{"id": "3", "url": "cam-e.jpg", "area": "2"},
{"id": "4", "url": "cam-f.jpg", "area": "2"}]
bulb_data = []
def framed_images(fake):
print(bulb_data)
if __name__ == '__main__':
print("camera_data - ",camera_data)
for data in camera_data:
bulb_data.append({"url": data["url"], "bulb": False})
print("bulb_data - ",bulb_data)
# framed_images()
fake_Data = bulb_data
with Pool(processes=4) as pool:
pool.map(framed_images, fake_Data)
I am getting output as:
camera_data - [{'id': '1', 'url': 'cam-c.jpg', 'area': '1'}, {'id': '2', 'url': 'cam-d.jpg', 'area': '1'}, {'id': '3', 'url': 'cam-e.jpg', 'area': '2'}, {'id': '4', 'url': 'cam-f.jpg', 'area': '2'}]
bulb_data - [{'url': 'cam-c.jpg', 'bulb': False}, {'url': 'cam-d.jpg', 'bulb': False}, {'url': 'cam-e.jpg', 'bulb': False}, {'url': 'cam-f.jpg', 'bulb': False}]
[]
[]
[]
[]
the last four empty list is from multiprocessing pool. I expect an output like this:
[{'url': 'cam-c.jpg', 'bulb': False}, {'url': 'cam-d.jpg', 'bulb': False}, {'url': 'cam-e.jpg', 'bulb': False}, {'url': 'cam-f.jpg', 'bulb': False}]
[{'url': 'cam-c.jpg', 'bulb': False}, {'url': 'cam-d.jpg', 'bulb': False}, {'url': 'cam-e.jpg', 'bulb': False}, {'url': 'cam-f.jpg', 'bulb': False}]
[{'url': 'cam-c.jpg', 'bulb': False}, {'url': 'cam-d.jpg', 'bulb': False}, {'url': 'cam-e.jpg', 'bulb': False}, {'url': 'cam-f.jpg', 'bulb': False}]
[{'url': 'cam-c.jpg', 'bulb': False}, {'url': 'cam-d.jpg', 'bulb': False}, {'url': 'cam-e.jpg', 'bulb': False}, {'url': 'cam-f.jpg', 'bulb': False}]
in order to edit the list of dictionary in each process while updating in global variable.