Let's say I have the following class defined:
class Animal:
def __init__(self):
self.isAlive = True
Along with the following function:
def Kill_Animal(animal):
animal.isAlive = False
Now, if I create a List of animals, as follows:
AnimalsList = [Animal() for i in range(0,5)]
If the function is applied to any instance of the Animal Class inside the list, the isAlive attribute gets changed to False. However, if I wanted to apply this function to this list and change its contents via the multiprocessing library, what would be the correct way to do it?
I have tried the following:
from multiprocessing import Process, Pool
pool = Pool()
pool.map(Kill_Animal, AnimalsList[0:3])
However, if i try checking the attribute for all the elements inside the list, the result is as follows:
[print(animal.isAlive) for animal in AnimalsList]
Output: True True True True True
Additionally, if I try checking the ID of the object that is passed to the Kill_Animal function during runtime via the Pool.Map, it does not match with the object's own ID. I am familiar with Python's call-by-object reference, but what is happening here?