Reputation: 23
A function is giving different results when I run it from one process from when I'm calling it with multiprocessing. I'm not sure why. I'm creating a list from the manager, and calling the same target function with different parameters for each process. The target function calls a function that is imported from a different module. It is the imported function that is giving me different results based on when I'm calling it from multiple processes or one process.
For example:
from foo import foo_function
from multiprocessing import Process, Manager
def another_function(indices, a_list, return_list):
for i in indices:
for j in a_list:
return_list.append(foo_function(i, j))
if __name__ == '__main__':
jobs = []
manager = Manager()
return_list = manager.list()
all_indices = [[0, 1, 2], [3, 4, 5], [6, 7, 8]]
all_lists = [['a', 'b', 'c'], ['d', 'e', 'f'], ['g', 'h', 'i']]
for i in range(3):
jobs.append(Process(target=another_function, args=(all_indices[i], all_lists[i])))
jobs[i].start()
for i in range(3):
jobs[i].join()
And foo_function() is giving me different results when I call it from multiple Processes vs when I call it from one.
EDIT: Here is what the actual foo_function is:
def battle(ally: CustomClass, enemy: CustomClass, shields: int):
ally.starting_shields = shields
enemy.starting_shields = shields
ally.reset()
enemy.reset()
turns = 0
# Main Battle Loop
while ally.is_alive() and enemy.is_alive():
ally.reduce_cooldown()
enemy.reduce_cooldown()
turns += 1
if ally.can_act():
if not ally.use_charge_move(enemy):
ally.use_move(ally.fast_move, enemy)
if enemy.can_act():
if not enemy.use_charge_move(ally):
enemy.use_move(enemy.fast_move, ally)
# There are 2 points for using enemy shields and 3 for using enemy health.
ally_rating = enemy.starting_shields - enemy.get_shields()
enemy_rating = ally.starting_shields - ally.get_shields()
ally_rating += 5 * (enemy.starting_health - enemy.get_health()) / enemy.starting_health
enemy_rating += 5 * (ally.starting_health - ally.get_health()) / ally.starting_health
if ally.get_health() > 0:
ally_rating += 3 * ally.energy / 100
if enemy.get_health() > 0:
enemy_rating += 3 * enemy.energy / 100
total_rating = ally_rating + enemy_rating
return int(round(1000 * ally_rating / total_rating, 0)), int(round(1000 * enemy_rating / total_rating, 0))
As you can see, it's only calling the CustomClasses methods and only uses local variables.
Upvotes: 1
Views: 118
Reputation: 23
The issue ended up being one not related to multiprocessing. Sorry.
Upvotes: 1
Reputation: 1188
Pretty hard to say without knowing foo_function
, but it's probably due to different processes accessing a reference to the same list and finding it to have different values for each, since there doesn't seem to be any concurrence handling there.
Upvotes: 0