I will be implementing multiprocessing so that the loops are occurring at the same time, but how can I make it so at the end of each iteration, I can obtain the value of westernEurope.cases
and easternEurope.cases
so that I can add them together
westernEurope = Region("Western Europe", 1000, 0, 0, 8, 4, 4, 0) while westernEurope.deaths < westernEurope.population: westernEurope.infection() if westernEurope.cases > westernEurope.population: westernEurope.cases = westernEurope.population print("Infections:", westernEurope.cases) westernEurope.death() if westernEurope.deaths > westernEurope.population: westernEurope.deaths = westernEurope.population print("Deaths:", westernEurope.deaths) #where i want to return the value of westernEurope.cases time.sleep(0.1) easternEurope = Region("Eastern Europe", 1000, 0, 0, 8, 4, 4, 0) while easternEurope.deaths < easternEurope.population: easternEurope.infection() if easternEurope.cases > easternEurope.population: easternEurope.cases = easternEurope.population print("Infections:", easternEurope.cases) easternEurope.death() if easternEurope.deaths > easternEurope.population: easternEurope.deaths = easternEurope.population print("Deaths:", easternEurope.deaths) # where i want to return the value of easternEurope.cases time.sleep(0.1) print(easternEurope.cases + westernEurope.cases)
Advertisement
Answer
IMHO there is no need for multiprocessing. With a generator, your problem can be solved in an even more elgant way.
# where i want to return the value of easternEurope.cases yield region.cases
Full code:
def desease(region: Region): while region.deaths < region.population: region.infection() if region.cases > region.population: region.cases = region.population print("Infections:", region.cases) region.death() if region.deaths > region.population: region.deaths = region.population print("Deaths:", region.deaths) # where i want to return the value of easternEurope.cases yield region.cases time.sleep(0.1) easternEurope = Region("Eastern Europe", 1000, 0, 0, 8, 4, 4, 0) westernEurope = Region("Western Europe", 2000, 0, 0, 8, 4, 4, 0) eastDesease = desease(easternEurope) westDesease = desease(westernEurope) for eastCases, westCases in zip(eastDesease, westDesease): print(eastCases, westCases)