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
JavaScript
x
28
28
1
westernEurope = Region("Western Europe", 1000, 0, 0, 8, 4, 4, 0)
2
while westernEurope.deaths < westernEurope.population:
3
westernEurope.infection()
4
if westernEurope.cases > westernEurope.population:
5
westernEurope.cases = westernEurope.population
6
print("Infections:", westernEurope.cases)
7
westernEurope.death()
8
if westernEurope.deaths > westernEurope.population:
9
westernEurope.deaths = westernEurope.population
10
print("Deaths:", westernEurope.deaths)
11
#where i want to return the value of westernEurope.cases
12
time.sleep(0.1)
13
14
easternEurope = Region("Eastern Europe", 1000, 0, 0, 8, 4, 4, 0)
15
while easternEurope.deaths < easternEurope.population:
16
easternEurope.infection()
17
if easternEurope.cases > easternEurope.population:
18
easternEurope.cases = easternEurope.population
19
print("Infections:", easternEurope.cases)
20
easternEurope.death()
21
if easternEurope.deaths > easternEurope.population:
22
easternEurope.deaths = easternEurope.population
23
print("Deaths:", easternEurope.deaths)
24
# where i want to return the value of easternEurope.cases
25
time.sleep(0.1)
26
27
print(easternEurope.cases + westernEurope.cases)
28
Advertisement
Answer
IMHO there is no need for multiprocessing. With a generator, your problem can be solved in an even more elgant way.
JavaScript
1
3
1
# where i want to return the value of easternEurope.cases
2
yield region.cases
3
Full code:
JavaScript
1
22
22
1
def desease(region: Region):
2
while region.deaths < region.population:
3
region.infection()
4
if region.cases > region.population:
5
region.cases = region.population
6
print("Infections:", region.cases)
7
region.death()
8
if region.deaths > region.population:
9
region.deaths = region.population
10
print("Deaths:", region.deaths)
11
# where i want to return the value of easternEurope.cases
12
yield region.cases
13
time.sleep(0.1)
14
15
16
easternEurope = Region("Eastern Europe", 1000, 0, 0, 8, 4, 4, 0)
17
westernEurope = Region("Western Europe", 2000, 0, 0, 8, 4, 4, 0)
18
eastDesease = desease(easternEurope)
19
westDesease = desease(westernEurope)
20
for eastCases, westCases in zip(eastDesease, westDesease):
21
print(eastCases, westCases)
22