简体   繁体   中英

When implementing Python's multiprocessing, function gives different return values?

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.

不知道foo_function很难说,但这可能是由于不同的进程访问了对同一列表的引用并发现每个列表具有不同的值,因为那里似乎没有任何并发​​处理。

The issue ended up being one not related to multiprocessing. Sorry.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM